Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handling Concurrent processes within VB6 3

Status
Not open for further replies.

R3D3

Programmer
May 1, 2002
34
FR
How can I create 2 different processes in VB6 that will run concurrently?

I have 3 functions A, B and C, each of them taking 5 seconds.
Function C uses the results of Functions A and B
Functions A and B are completely independant of each other and their processing is performed on different machines so I should be able to improve the performance by about 30%

How can I start function B before I receive the result from Function A?


 

Something crazy would be a case where the first one could raise an event back to the caller, and then the caller calls the second function from that event.

Put each of them in their own ActiveX Exe so they can be ran simutaniously.
 
Functions A and B are already both in separate DLL's so it should be possible to solve this without creating separate EXE's.
Even with separate EXE's I have the same issue...
My problem is how to wait for the results of the two responses before continuing with the processing.
 
You need to have your two worker processes set a boolean value to True when they complete. You can then wait on them by doing code similar to this:
Code:
Do
   Do Events
Loop While Not (bProcessAComplete And bProcessBComplete)
Chip H.
 

How can I create 2 different processes in VB6 that will run concurrently?

You can't within a single VB program! VB 6.0 is single threaded so your execution would be A>B>C, BUT!!!! you can make a seperate exe and call it (search for shell and wait any date) and wait on those external processes to finish before you process C.

Good Luck

 
"Even with separate EXE's I have the same issue"

But can't they (activex exe) run on seperate threads like a normal exe.
 

VBOldTimer,

With the ShellExecuteEx API you can shell out a process and wait on it to complete (Note: the other process needs to be self termininating or primary exe will wait forever). If you search for "shell and wait" using all words and any date the first three answers have enough info in them for you to be able to...

Start Primary Exe (A)
Primary Exe Starts Secondary Exe (B)
Primary Exe Starts Secondary Exe (C)
Primary Exe Waits On Either (C or B)
Once (C or B) is complete Primary Waits On the Other Exe (B or C)
Once (B or C) Second Secondary Process Primary Then Can Do Process Its Primary Process

Now with with ActiveX Exe I believe we are talking about the same thing just about, just using different technologies or methods. The difference is I am talking about spawning a process in its own thread space (apartment) instead of having another thread in my apartment.

I hope I explained that well....

Good Luck

 
" hope I explained that well"

Yes you did vb5prgrmr. Thankyou.
 
Ok. Now I understand the Shell & CreateProcess but I dont seem to be able to apply it to my needs.

I have the processes in a library so it is the function call that I need to 'shell' rather than an exe or command.


Set A = New COMLib.API
Set B = New COMLib.API

Set oResponse1 = A.Fares(oRequest1)
Set oResponse2 = B.Avail(oRequest2)

As far as I can see there is no way to start the
Set oResponse2 = B.Avail(oRequest2)
until I receive the oResponse1

or am i missing something?



 

I am not sure that carefully is a strong enough word there strongm!

 
Mwahahah! Here's some example code that illustrates the idea...

You'll need a form with a single button, and the following code:
[tt]
Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Command3_Click()
Dim hThread As Long
Dim a As New Wombat
Dim b As New Wombat
Dim starttime As Long

' techniqe that blocks
starttime = GetTickCount
oResponse1 = a.FiveSecs
oResponse2 = b.TenSecs
Debug.Print GetTickCount - starttime

Debug.Print "Both calls now finished..."
oResponse1 = 0
oResponse2 = 0

' Horrendous thread-based technique that doesn't block...
starttime = GetTickCount
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf FiveSecs, ByVal 0&, ByVal 0&, 0&) ' hThreadID)
' Don't need handle, so dispose of it cleanly
CloseHandle hThread
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf TenSecs, ByVal 0&, ByVal 0&, 0&) ' hThreadID)
' Don't need handle, so dispose of it cleanly
CloseHandle hThread
Do Until oResponse1 <> 0 And oResponse2 <> 0
DoEvents
Loop
Debug.Print GetTickCount - starttime

Debug.Print &quot;Both threads now finished...&quot;
oResponse1 = 0
oResponse2 = 0
End Sub
[/tt]

And a module containing the following:
[tt]
Option Explicit

Public Declare Function CreateThread Lib &quot;kernel32&quot; (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Public Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long
Public oResponse1 As Long
Public oResponse2 As Long

Public Sub FiveSecs()
Dim a As New Wombat
oResponse1 = a.FiveSecs

End Sub


Public Sub TenSecs()
Dim b As New Wombat
oResponse2 = b.TenSecs
End Sub
[/tt]

And, for the example, a class module (my class is imaginatively called 'Wombat') with the following code:
[tt]
Option Explicit

Private Declare Function GetTickCount Lib &quot;kernel32&quot; () As Long

Public Function TenSecs() As Long
Dim starttime As Long
starttime = GetTickCount
Do Until GetTickCount - starttime >= 10000
Loop
TenSecs = 10
End Function


Public Function FiveSecs() As Long
Dim starttime As Long
starttime = GetTickCount
Do Until GetTickCount - starttime >= 5000
Loop
FiveSecs = 5
End Function
 
New Wombat LOL seriously?!?!?

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 

I'll have to say that is one of the better examples I have seen (meaning it did not crash nor did it give me a GPF or the BSOD) on using threads in VB6.0. I normally try to stay away from using threads in VB6.0 (bad, very bad experience) and since this is an open forum I tend to shy away from these potentially dangerous solutions, but with all that said here is a star. ;-)

(Ok the Mark this post as a helpful/expert post shells out a new window with the home page!!!!)

When its fixed I'll try to remember.

Cool!

 
>New Wombat

Yep! Actually, it's a wierd thing. When I am rapidly putting together test/example code I tend to revert to 'bad' programming practices, and just use stuff that I'm comfortable with (Wombat is one example of this and, for reasons I cannot even begin to explain, HappyFish is another...)
 
lol Im a jobby and wotsit kinda guy!! lol

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
 
May I add to the great wombat debate...

I must me very conventional (and retentive) as I use foo and foobar


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 

And may I add to the Foobar debate (or start one). That should be fubar (F..... Up Beyond All Recognition) LOL.

 
Actually,
Code:
fubar - Fouled Up Beyound All Recognition
and
Code:
foobar - The universal variable for whatever is being discussed
are both correct.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top