Multi-threaded VBA (CreateThread)

tracks

New Member
Joined
Aug 31, 2005
Messages
21
We'll I've had some successes with the CreateThread APIs natively in VBA. It's not crashing excel, although there are some anomalies that need to be worked on.

Highlights:
  • 1MM concurrently running asynchronous threads--success!

Problems/Issues:
  • I can't seem to pass a value/pointer to the function. CreateThread will accept lpParameter, but the function doesn’t seem to receive it. As long as I don’t attempt to read the variable/pointer in the function, it’s fine. But I so much as put it in a msgbox, instant crash. As far as I can tell, the passed variable just doesn’t exist at runtime.

    Still validating the resources situation. If I run too many concurrent threads, excel will sometimes behavior strangely after the fact (and all threads are closed). Everything seems to work ok, but for example: if I click save, sometimes I get the error message “Microsoft Excel cannot accesss the file … . There are several possible reasons (1) The file name or path does not exist. (2) The file is being used by anthor program (3) the workbook you are tyring to save has the same name as a currently open workbook." I am concerned that some memory might be overwritten somehow.

    ExitThread does not work, which might be why the reason for some of the anomalies. Using TerminateThread instead, which may or may not be freeing up the reasources. Memory usage in task manager appears "normal"

    Don't write to the same cell simultaneously--crash.



I'm most concerned with passing a value to the new thread without resorting to globals. Any insights?


Here's some sample code :biggrin: (note: i left out the var in the function declaration, as well as some other testing lines)

Code:
Function AsyncThread()
    On Error Resume Next
    
    ' Do Stuff

    ' Clean up Thread
    AsyncThread = True
    Dim lpExitCode
    GetExitCodeThread GetCurrentThread, lpExitCode
'    ExitThread lpExitCode ' Crashes Excel
    TerminateThread GetCurrentThread, lpExitCode
End Function
Sub a_test()
    On Error Resume Next
    
    Dim hThread
    For i = 1 To 100000
        hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, 0&, ByVal 0&, 0&)
        CloseHandle hThread
    Next i

    MsgBox "Done"
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why do you need another thread? What is this accomplishing? Is there a problem to solve here or is this just for your own curiosity. I am asking because there are probably safer alternatives.
 
Upvote 0
Good question: this for is for two internet VBA applications. Multi-threaded offers huge benefits and significant productivity ROI (if it can be done). It's promising enough that it's worth pursuing a bit more at least.
 
Upvote 0
Have you considered using multiple instances of Excel or creating an activeX.exe server that will run in a seperate process? There are other alternatives such as creating script on the fly.


Some examples of using a separate instance of Excel that resembles the behavior of an activeX server.

Communicating between two excel applications
asynchronous communication between Excel instances

'A' starts a procedure in 'B'. 'A' goes about it's business. 'B' notifies 'A' when it has finished.


"this for is for two internet VBA applications"
Is vague.

I can't help you with the CreateThread API but I may have some other ideas. Personally, I would not even attempt to make VBA multi-threaded. At least not in any project I intended on releasing for distribution.

You may wish to PM Jaafar as he may possess more knowledge and interest regarding this post.
 
Upvote 0
Interesting techniques... I like the idea of multiple instances. I'll definitely play around with it. Thanks!


As far as CreateThread goes, it still feels like this can be done. Unfortunately, it will be about a month before I can dig into again. I'll post any progress here though.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top