Solved

multithread in VB6.0

Posted on 2002-07-08
11
962 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA: Add rows to listbox based on criteria 7 80
VBA error replacing data 6 37
VB6 Compile Compatibility Issue 4 91
Advice in Xamarin 21 57
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

863 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

17 Experts available now in Live!

Get 1:1 Help Now