Difference between revisions of "Multithreading"

From HashVB
Jump to: navigation, search
(Initial chat log)
 
(BIG tidyup)
Line 1: Line 1:
<@QueenDee> teh easiest way is to use an AX exe
+
The easiest way to do multithreading in VB is to make use of ActiveX EXEs and "out of process" components.
<@QueenDee> let me explain
+
 
< LindaBob> ouch, I hate seeing shut up, but seeing you two, I can tell there is respect in that request
+
When you have an ActiveX EXE set to "Thread per object", every object instantiated will in a new thread in the EXEs process and all calls/events marshalled across the thread boundary. This ONLY applies when the object is created through the normal COM calls (CreateObject). If you use Set Variable = NewObject then VB will bypass all the COM stuff and create it internally, in the same thread.
<@QueenDee> sorry :)
+
 
* Joey__ is shutting up whilst Dee is explaining :)
+
So, you now have this object successfully in another thread. Lets make a call into it to do something. Oh. Your original code is still blocked while calling this other thread. This is perfectly normal and allows you to get the return value (This is called the thread of execution and has nothing to do with multi threading)
< Joshuarez> o.O
+
 
* LindaBob is doing the same, short of making sure this is ONLY for case of AX EXE
+
If you want to allow the main process to continue, you will need to exit from the "DoSomething" method and all it to continue. But this leaves you without any code running in your new thread....
* Joey__ doesn't mind that Dee is bossing him arround, he kinda likes in it in a weird way
+
 
<@QueenDee> with thread per object, it will do as you say and each object will be in a new thread and all calls marshalled
+
This is where SetTimer() comes in useful with a few tricks to be able to call another method in your object allowing both to continue as normal. You can then use normal COM calls to communicate between the threads. Please note that if you have an long process running, you will need to call DoEvents periodically to allow calls to be processed.
            accross but ONLY if created from outside
+
 
<@QueenDee> liek createobject
+
Enjoy :)
* LindaBob doesn't explore that last post
+
<@QueenDee> using set blah = new object bypasses it and it is the same thread
+
<@QueenDee> so, you now have this object in another thread
+
< LindaBob> QueenDee - thread per object is called ?  btw... Is that Apartment ? I forget the two types, Apartment vs ?
+
<@QueenDee> let me finish
+
< LindaBob> ok
+
<@QueenDee> it is still blocking the call from the main code so you need to start a callback timer (settimer api call or maybe
+
            even a timer control if you are luicky)
+
<@QueenDee> return form the first function, the main process will then go off and do its stuff
+
-!- G_AME [~icegoldgr@216.110.101.79] has joined #vb
+
* LindaBob remembers this now, you use the AX EXE as an 'outside instantiator'
+
-!- mode/#vb [+l 33] by X
+
<@QueenDee> meanwhile in your new threadm teh timer calls back and taht will go off on its own
+
<@QueenDee> you can then use COM to communicate between them
+
< LindaBob> ya ya ya
+
<@QueenDee> any questions? :)
+
< LindaBob> yes
+
< TwOOftens> whats COM?
+
< LindaBob> the original question was just a class instance
+
< LindaBob> TwoOftens - Component Object Model
+

Revision as of 03:00, 9 November 2005

The easiest way to do multithreading in VB is to make use of ActiveX EXEs and "out of process" components.

When you have an ActiveX EXE set to "Thread per object", every object instantiated will in a new thread in the EXEs process and all calls/events marshalled across the thread boundary. This ONLY applies when the object is created through the normal COM calls (CreateObject). If you use Set Variable = NewObject then VB will bypass all the COM stuff and create it internally, in the same thread.

So, you now have this object successfully in another thread. Lets make a call into it to do something. Oh. Your original code is still blocked while calling this other thread. This is perfectly normal and allows you to get the return value (This is called the thread of execution and has nothing to do with multi threading)

If you want to allow the main process to continue, you will need to exit from the "DoSomething" method and all it to continue. But this leaves you without any code running in your new thread....

This is where SetTimer() comes in useful with a few tricks to be able to call another method in your object allowing both to continue as normal. You can then use normal COM calls to communicate between the threads. Please note that if you have an long process running, you will need to call DoEvents periodically to allow calls to be processed.

Enjoy :)