Thread and callback

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

When creating a thread, how do I make it loop until a specific condition
happen to make it end?

Just for instance, I want a thread to loop until (appl. is closed) to
perform actions on items within a collection.

Does it need a callback function?

Thanks you

Marty
 
When you start your thread, you chose what procedure it starts... However
you can pass in a state object (using thread pool) or set whatever
properties you want to a custom class using the Thread.Start method (which
has no parameters allowed to be sent in, but you can just set these in
properities)

The easiest way to do it from that point is adding a andling to
Application.Exit, Application.Exception, and Application.ThreadException
that will then notify the class that is running on the separate thread that
something died. and go from there. But you still need to have a loop that
says

While not bHasExited

'WARNING put in a sleep timer here because it will eat up a lot of cycle
time just looping.

Wend
 
Hi

you can periodically check a boolean variable in the loop of the thread and
if it is true, exit the loop. Or you can wait very short time for an event
and if it is signaled by application you exit the loop. Another option is
sending a message to the thread by PostThreadMessage API which informs it to
exit the loop. In the last option you need to check your message queue. In
all the ways you should Join to this thread in main application thread to
make sure it is finished before closing the application.
 
Hi

you can periodically check a boolean variable in the loop of the thread and
if it is true, exit the loop. Or you can wait very short time for an event
and if it is signaled by application you exit the loop. Another option is
sending a message to the thread by PostThreadMessage API which informs it to
exit the loop. In the last option you need to check your message queue. In
all the ways you should Join to this thread in main application thread to
make sure it is finished before closing the application.
 
Hi

you can periodically check a boolean variable in the loop of the thread and
if it is true, exit the loop. Or you can wait very short time for an event
(autoreset or manual) and if it is signaled by application you exit the loop.
Another option is sending a message to the thread by PostThreadMessage API
which informs it to exit the loop. In the last option you need to check your
message queue. In all the ways you should Join to this thread in main
application thread to make sure it is finished before closing the application.
 

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

Similar Threads

Threads and Callbacks 19
WCF singleton service + callbacks 0
Callback from a thread 4
Threading issue 3
background threads and keeping the UI painting 5
multi-tasking question 4
Threading 3
Multi-Threading 9

Back
Top