End Processing in Multithreaded App

J

Jay

How do I stop execution of remaining sub procesures in a multithreaded app?
For example:

sub form_load
dim i as integer
for i = 1 to 5 '5 threads
dim ts as threadstart(addressof classname.procwhatever)
dim wthread as thread(ts)
classname.currentthread=wthread
whread.setapartmentstate(apartmentstate.sta)
wthread.name=i.tostring
wthread.start()
next

sub procwhatever 'executes at thread start
call proc1 '(may also be a function call)
if somecondition=true then
'need to stop thread from processing remaining subs (proc2, proc3)
end if
call proc2
if somecondition=true then
'need to stop thread from processing remaining subs (proc3)
end if

call proc3
end sub

sub proc1
'do stff here
end sub

sub proc2
'do stff here, etc.
end sub

sub proc3
'do stff here
end sub

So is somecondition is true in the procwhatever I need to stop execution of
the remaining subs but leave the app running. This app will end up a
windows service. I thought of just "Exit sub" however, sub1 might call
sub2. Sub2 may call sub55, sub 55 may call sub3, etc, etc. So I need to
end the processing and return the app to an idle state pretty much anywhere
in the code. Is it possible to tell the threads remaining operations to
stop? I still do need to keep the thread active but just stop execution of
remaining code. any thoughts?



end sub
 
N

NickP

Hi there,

In this case you would use a ManualResetEvent object.

Get a thread that isn't busy to signal the event, then the event can
acknowledge it and reset it again as it exits.

e.g.

-------

Thread 1

1) Create thread 2
3) Do stuff
4) Set event

Thread 2

1) Do stuff
2) Check if event is set, if so go onto 3, otherwise loop
3) Reset event

-------

If you are ever to have a reliable multi-threaded application it must
use some form of synchronisation, these are really good and easy to use!

Nick.
 
J

Jay

Thanks a lot Nick. Would you happen to know of examples illustrating this
type of behaviour? I've been searching the net however can't find much.
 
S

Stephany Young

All you need to do is reverse your test of 'somecondition':

Sub procwhatever 'executes at thread start

proc1()

If Not somecondition Then proc2()

If Not somecondition Then proc3()

End Sub

or, you could short-circuit it by:

Sub procwhatever 'executes at thread start


proc1()

If Not somecondition Then
proc2()
If Not somecondition Then proc3()
End If

End Sub

or even, with the original test:

Sub procwhatever 'executes at thread start


proc1()

If somecondition Then Return

proc2()

If somecondition Then Return

proc3()

End Sub

You have to be aware though that when you Return from procwhatever or
procwhatever reaches the end of it's logic the thread will 'die'. Once all 5
threads have 'died' all you will have is an application with a form that is
doing absolutely nothing.

If you translate this to a service then you will end up with a service that
is running but doing nothing.

You need to start researching the mechanisms available for keeping threads
alive (and responsive) and for terminating threads cleanly when a service
needs to be stopped for any reason.
 
C

Cor Ligthert [MVP]

Hi Nak,

Beside you is Tom Leylan again active her, he was asking if we had ever
heard something from Fergus, I told that even you did not, was I right?

Cor
 
N

NickP

Hey Cor,

Unfortunately I haven't heard anything from Fergus in a long time. That was
a funny time in the newsgroup!

How are you?

Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top