Difference between revisions of "Multithreading"

From HashVB
Jump to: navigation, search
(Initial chat log)
 
m (Reverted edit of Christine, changed back to last version by Dee)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<@QueenDee> teh easiest way is to use an AX exe
+
{{VB6}}
<@QueenDee> let me explain
+
{{For VB.NET|multithreading|.NET Threading}}
< LindaBob> ouch, I hate seeing shut up, but seeing you two, I can tell there is respect in that request
+
 
<@QueenDee> sorry :)
+
The easiest way to do multithreading in VB is to make use of ActiveX EXEs and "out of process" components.
* Joey__ is shutting up whilst Dee is explaining :)
+
 
< Joshuarez> o.O
+
== Out of process COM Objects ==
* LindaBob is doing the same, short of making sure this is ONLY for case of AX EXE
+
 
* Joey__ doesn't mind that Dee is bossing him arround, he kinda likes in it in a weird way
+
When you have an ActiveX EXE set to "Thread per object", every object instantiated will be 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> with thread per object, it will do as you say and each object will be in a new thread and all calls marshalled
+
 
            accross but ONLY if created from outside
+
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)
<@QueenDee> liek createobject
+
 
* LindaBob doesn't explore that last post
+
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....
<@QueenDee> using set blah = new object bypasses it and it is the same thread
+
 
<@QueenDee> so, you now have this object in another 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.
< LindaBob> QueenDee - thread per object is called ?  btw... Is that Apartment ? I forget the two types, Apartment vs ?
+
 
<@QueenDee> let me finish
+
== CreateThread() and VB ==
< 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
+
You may have heard that CreateThread() is "Bad" in VB? :)
            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
+
It is possible to use BUT it requires a lot of work setting up Thread Local Storage (TLS) before you do anything else. Without TLS, VB won't do much beyond beep and crash. Even after setting up the TLS, you will need to handle thread synchronisation yourself.
-!- G_AME [~icegoldgr@216.110.101.79] has joined #vb
+
 
* LindaBob remembers this now, you use the AX EXE as an 'outside instantiator'
+
I will leave the details for later (or for someone else to fill in)
-!- mode/#vb [+l 33] by X
+
 
<@QueenDee> meanwhile in your new threadm teh timer calls back and taht will go off on its own
+
Enjoy :)
<@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
+

Latest revision as of 08:54, 12 August 2010

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.
 For information on multithreading in Visual Basic.NET, see .NET Threading.

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

Out of process COM Objects

When you have an ActiveX EXE set to "Thread per object", every object instantiated will be 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.

CreateThread() and VB

You may have heard that CreateThread() is "Bad" in VB? :)

It is possible to use BUT it requires a lot of work setting up Thread Local Storage (TLS) before you do anything else. Without TLS, VB won't do much beyond beep and crash. Even after setting up the TLS, you will need to handle thread synchronisation yourself.

I will leave the details for later (or for someone else to fill in)

Enjoy :)