Solved

multithread in VB6.0

Posted on 2002-07-08
11
960 Views
Last Modified: 2007-12-19
How do I create multithread in vb6.0 ?
Let say, I need two threads to run and increment from
1 to 100000. Each incremet should display on the textbox.
Thread1 will be displayed on text1, and thread2 will be
displayed on text2.
0
Comment
Question by:j_script
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 7138832
Microsoft changed the threading model in VB 6 so that you cannot call the CreateThread API.  You have 3 or 4 choices:

You can develop under VB 5 which will allow you to use the CreateThread API.

You can develop under VB.NET which I think supports multi-threading.

You can download a multi-threading Active-X control developed in C or C++ (which do support multi-threading threading models).  I know that there are some available for a fee, but I don't know of any freeware controls.

You can try to implement the "poor man's multi-threading" which involves using timer controls.  An example of this would be to set two timer controls on your form.  Set the interval of each to 1 but set the enabled to false.  In each Timer's Timer_Event, put the following code:

Static lCount as long

  lCount = lCount + 1
  if lCount = 100000 then
     Timer1.Enabled = false ' mention timer2 for the second timer
  Else
     Text1.Text = CStr(lCount) ' mention text2 for the second timer
  End if

Then, under the button that kicks off the process just say:

timer1.enabled = True
Timer2.Enabled = True
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7138833
Actually, if you want to be able to click the button more than once you'd need to do:

if lCount = 100000 then
    lCount = 0
    Timer1.Enabled = false ' mention timer2 for the second timer
   
0
 

Author Comment

by:j_script
ID: 7138885
I don't want to use Timer. I did try to do it in VB.Net,
sometime, the threads just stop for no reason.
0
 

Author Comment

by:j_script
ID: 7138888
I don't think vb.net is a stable tool.
0
 
LVL 5

Accepted Solution

by:
rpai earned 100 total points
ID: 7138940
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:xSinbad
ID: 7139111
0
 

Author Comment

by:j_script
ID: 7140733
rpai,

I have downloaded the link you provided and ran it in vb6
and it worked.

I thought thread only works in vb5. Why it still work in
vb6 ?
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7141248
I downloaded it too, and it worked.  Surprising, as another example that I had a while back using the same APIs crashed.  I was on Windows 2000 testing the other project and Windows NT4 testing this one.  That could be one difference.  

Also, In my other example that didn't work, I might have been using callbacks, meaning that when a thread finishes processing, it calls back to the main thread to let it know that the thread is done.  In this test project, the threads run indefinitly until they are stopped.

Make sure to test from the compiled EXE.  I was always able to run my other code through the VB IDE, but as soon as I compiled it to EXE, then it would crash on start up.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7141255
Oh, and also, I'm pretty sure that the thread priorities wont work on any system newer than Windows 95.
0
 

Author Comment

by:j_script
ID: 7141334
How do I really know the thread is terminated ?
0
 
LVL 5

Expert Comment

by:rpai
ID: 7141524
In the clsThreading.cls module, take a look at the TerminateCurrentThread function. This function uses the TerminateThread function:-

Call TerminateThread(mThreadHandle, ByVal 0&)

TerminateThread returns a nonzero value, on success.
If the function fails, the return value is zero.
So you could replace the above statement by this

Dim iRetVal as Integer

iRetVal = TerminateThread(mThreadHandle, ByVal 0&)

If iRetVal = 0 then
  '-- Error
Else
  '-- The thread has really terminated
End If
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now