Link to home
Start Free TrialLog in
Avatar of Greg
GregFlag for Canada

asked on

Threading a vb app

I want to be able to make a vb program with multiple threading.   I would like sample code for 1 form and 1 class where the form calls the class procedure in a new thread and runs it in the background until its finished or the user presses cancel on the form.  

Is this possible and how.
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marshawk
Marshawk

Sure its easy -- sortof. Bbut you will have to maintain more than one exe files.

First, create a class..in an activex exe...set the threading model to "thread per object"
This will be your application support class where the background threaded processing occurs
You will need to use createobject from your normal vb app to create you support class.

in your class create a stub public method... like this:
We are assuming that we are burning a cdrom on a seperate thread.


Public Sub BurnCDAsync()
  'just set a timer and return immediately to the normal vb app caller
   Set Timer1 = CreateObject("vbTimer.TimerClass")
   Timer1.Interval = 200
   Timer1.Enable = True
End Sub

The public method is strictly for applying a way for the caller request a threaded activity and detach. The stub public method returns immediatley to the calling normal exe.

The stub process set a time and it is in this timer event where the real code on its own thread will execute.



(actual detached seperate thread runs in the timer event)

Private Sub Timer1_Timer()
   
   Timer1.Enable = False     'turn the timer off since its job of getting us here is done
   Set Timer1 = Nothing
 
   .....real work done here on a detached seperate thread....
   .....for example burn a cdrom, crawl a website or whatever...
end sub


you raise and event form this support class to notify your normal exe when the separate thread processing is done.

Now usually i put now forms in my threading support class, so you will need to write a timer using the win32 timer api or find one on the internet. If you go this route and can't find a sufficient timer object on the net, I can try dig up the one I used to use.


once you do a few its a breeze. But its a little bit of a maintance and development hassle cause you are working with the activex support exe and your normal vb application exe and coding and maintaining both. However the techinque is sound and is the best way I have found to run length background processes asynchronously with VB6.

Hope I explained it all well enough to give you the general idea.

Now I am using vb.net thank goodness.
sorry for the spelling errors.