?
Solved

multithread in VB6.0

Posted on 2002-07-08
11
Medium Priority
?
967 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
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!

 

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

Technology Partners: 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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

752 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