G
Guest
Hi:
I have a class that needs to request/receive updates from a server over a
network connection for the entire life of the application. I use a
background worker thread to do this, and then send updates to the main thread
using events. This works great.
My issue is that when a user wants to terminate the application, the thread
could be (will be) in any number of blocking calls, for example
BinaryReader.ReadByte. Almost all of what my background thread does is pull
data from the server via a TcpClient > Stream > BinaryReader.
I've done lots of reading on the topic, and everyone seems to say that I
sould use a ManualResetEvent and have the thread check periodically that this
event hasn't been Set. I do this now.
The problem is that even though I check to see if the event has been set,
the next line of code will start a blocking read.
I don't want to use Thread.Abort, and I don't want to switch to non-blocking
calls on the socket.
What am I missing?
Thanks.
Dave
P.S. Why not use a bool instead of a ManualResetEvent? Are value types not
thread safe?
I have a class that needs to request/receive updates from a server over a
network connection for the entire life of the application. I use a
background worker thread to do this, and then send updates to the main thread
using events. This works great.
My issue is that when a user wants to terminate the application, the thread
could be (will be) in any number of blocking calls, for example
BinaryReader.ReadByte. Almost all of what my background thread does is pull
data from the server via a TcpClient > Stream > BinaryReader.
I've done lots of reading on the topic, and everyone seems to say that I
sould use a ManualResetEvent and have the thread check periodically that this
event hasn't been Set. I do this now.
The problem is that even though I check to see if the event has been set,
the next line of code will start a blocking read.
I don't want to use Thread.Abort, and I don't want to switch to non-blocking
calls on the socket.
What am I missing?
Thanks.
Dave
P.S. Why not use a bool instead of a ManualResetEvent? Are value types not
thread safe?