Solved

Multithreading in VB6

Posted on 2002-04-18
10
1,568 Views
Last Modified: 2012-06-21
Does anyone know how to do multithreading in VB6?  For example, if I want to call a public function Process1()during form load, I want the form keep loading without waiting on Process1() to finish.  Actually, I have a case the Process1() will be running during whole run time.  It is like sth below:

Public Function Process1(arg1 as String)
do while True
......
loop
End Function

So, is there a way to use multithreading for this?  I'd like to use it inside of the code, instead of creating an ActiveX.

Working sample code needed please.

Thanks!
hxia

0
Comment
Question by:hxia
10 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
It will be hard for a VB6 app to get multi threading. BTW, VB.net supports it natively.

have a look at
http://www.desaware.com/articles/threadingL3.htm
http://www.desaware.com/articles/Waitingl3.htm
0
 
LVL 2

Expert Comment

by:Ber
Comment Utility
0
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
spyworks from www.desaware.com may help you to do it.

http://www.desaware.com/BackThreadL3.htm



0
 
LVL 15

Accepted Solution

by:
ameba earned 150 total points
Comment Utility
1. In Form_Load start your "long task" asynchronously.
2. In the loop in Process1() add enough DoEvents lines (e.g. 10 DoEvents per second).

For 1 you'll need a timer.  To prevent reentering Process1() or to signal task has finished, you'll need a flag (Boolean variable).

' in Form_Load
    ReadSomething ...
    Timer1.Enabled = True    ' this will start Process1
    DoSomething ...
    Show
    Refresh

' in Timer1_Timer
    Timer1.Enabled = False
    If bExecuting Then Exit Sub
    bExecuting = True   ' set flag, defined in a module or form
    Call Process1()
    bExecuting = False  ' reset flag

' in Process1
Public Function Process1(arg1 as String)
    Dim somecounter As Long
    Do
        somecounter = somecounter + 1
        If somecounter Mod 100 = 0 Then
            DoEvents    ' allow other tasks
        End If

        .......

        If bDone Then Exit Do
    Loop
End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
learning...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:hxia
Comment Utility
DoEvents is the easiest solution to my problem, but not the most efficient.  
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Experts here very much appreciate comments when they have helped you and the results are less than "A" level results.  We all take pride in delivering excellence and appreciate feedback from you anytime we fall short of those expectations so that the "missing" elements or added insights can be added.  Why did you choose to grade less than an "A" here, please?

To add a little more explanation on grading:  The goal on grading is to assess the value given in the question, and to give it the level of quality that will entice others to visit the solution if it served you once the question has migrated to our PAQ.  People are more likely to visit a question in our PAQ that is an A question grade rather than one which is a lower grade.  In addition, the points you give a question remain the same, but the points which flow to the expert are a direct result of the grade assigned.  Example:  100 points at an A grade = 400, B = 300, C=200

Listening further.  If no response will assume the grade was chosen in error and will correct.

Moondancer - EE Moderator
0
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility

Visual Basic allows you to launch components in background threads when using ActiveX EXE servers, however it does not allow this when using ActiveX DLL's or regular exe. Some programmers have attempted to use the CreateThread API to perform background thread operations with VB, but the results range from unsatisfactory to disastrous (refer to our article "A Thread to Visual Basic" for details).

The dwBackThread component makes it easy for the first time to launch objects in background threads in your ActiveX DLL components which support background (asynchronous) operations.

The dwBackThread component follows all of the COM threading rules necessary to make an object work reliably with Visual Basic. It performs cleanup tasks to allow your background thread to terminate correctly even if you don't release all of your objects properly.

The Desaware background thread component is included with SpyWorks Professional 6.2 and later and the NT Service Toolkit 1.0 and later.

http://www.desaware.com/BackThreadL3.htm
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Based on review and feedback from other moderators, the grade is changed.

Please see below.

Computer101
E-E Moderator



How Do I Know What Grade to Give?
Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:

A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Remember, the Expert helping you today is probably going to be helping you next time you post a question. Give them a fair chance to earn an 'Excellent!' grade and they'll provide you with some amazing support.

0
 

Expert Comment

by:NoCloneGroup
Comment Utility
ameba's answer is insightful but critical section is not considered so when accessing to shared variable, be careful. To avoid this, you have launch thread i.e. timer that do not use shared variable.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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

743 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

8 Experts available now in Live!

Get 1:1 Help Now