Solved

multithread in VB6.0

Posted on 2002-07-08
11
963 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…

770 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