Stopping thread does not work
I have a method that listens for data send through a stream. because the networkstream.read meth开发者_开发问答od is synchronous I have to execute that on a separate thread. so the method that I execute on a separate thread is:
void someMethod(param1, param2....)
{
try{
while( stream.read(data...) != 0 ) // code waits here until data is received
{
// do stufff
}
}catch{}//
}
the only way I am able to exit that thread is by closing the stream and the catch block will execute. I sometimes need to start listening for data on the same stream but on a different method. so if I create the thread as Thread t = new Thread(new threadstart(somemethod......) then do t.start( someObject); later when I do t.abort() ; that method will still be listening for data. How can I terminate it?
also I have tried creating a global variable such as:
bool someBoolean = false;
void someMethod(param1, param2....)
{
try{
while(someBoolean ==false && stream.read(data...) != 0 ) // code waits here until data is received
{
// do stufff
}
}catch{}//
}
then I figured that I changed someBoolean to true the method will stop executing. for some reason it does not. Why? it seems like someBoolean is two variables at once. Because changing its value from the main thread does not seem to impact it on the second thread...
I guess the thread might be abortet but then the resources from that thread (unmanaged because of stream) will not be freed - that is your problem.
Don't stop the thread with abort - never.
Do stop it try using a async version of stream.read together with a CancellationToken. If you give us more info (what stream? - read is strange ("R"ead?), starting of the thread) I might try to give you more details.
PS: try using System.Threading.Task instead of threads - way nicer to work with.
as a quick fix without further info, you could try something like this:
ManualThreadEvent terminate = new ManualThreadEvent(false);
bool someBoolean = false;
void someMethod(param1, param2....)
{
try{
// wait for enough data or for termination:
while(terminate.WaitOne(100) == false && stream.DataAvaiable <= bytesNeeded)
{ /* nothing to do here */ }
if (terminate.WaitOne(0)) return; // terminate on request
// To your stuff with read
}catch{}//
}
void Terminate()
{
terminate.Set();
}
How can I terminate it?
The standard protocol for unsticking a blocked stream call is to close the stream and catch the exception. Interrupting a read just so you can move it off to a different method is fairly unusual. If you feel like this is still necessary then you should provide an extra level of indirection on the read operation. It is a lot easier to deal with scenarios like this when using asynchronous calls like BeginRead
as opposed to the synchronous calls. Terminating a thread via Thread.Abort
can cause a lot more problems than it solves.
then I figured that I changed someBoolean to true the method will stop executing. for some reason it does not. Why? it seems like someBoolean is two variables at once. Because changing its value from the main thread does not seem to impact it on the second thread...
It is possible that a lack of a memory barrier can cause this problem, but the more likely cause is that the thread is stuck inside Read
and is not actually reading someBoolean
the way you hoped it would.
精彩评论