Solved

multithread in VB6.0

Posted on 2002-07-08
11
966 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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