How to stop a blocked worker thread?

O

Ole

CF2.0
VS2005
C#

How to stop a worker thread that is e.g. listening for data on a port when
the application is closed?

Thanks,
Ole
 
G

Guest

1. Make it a Background thread (set the IsBackground proeprty)
2. Use timeouts in your wait that periodically come back to see if it should
end
3. Wait on multiple items, like data receive and a quit event

-Chris
 
A

AC

Chirs' suggestion 1 should be used carefully as it might still
block the process from exit, if there's a blocking call into
native code going on.

In your particular case where you're listening to data on a port,
I'd recommend Close/Dispose the socket from your main thread,
which will give an ObjectDisposedException in the background
thread that you should handle gracefully.
 
O

Ole

The "socket.Receive" method doesn't include a time out parameter so are
there another ways to do it?

Thanks
Ole
 
G

Guest

Use BeginReceive, which you can stop when the app closes by calling Close on
the socket.

-Chris
 
O

Ole

Just setting it to be a backgroundthread seems to work - the app closes
without having to do anything else.

Thanks a lot
Ole
 

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