Using a Cross Thread Boolean to Abort Thread

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-03-25T09:08:07Z Indexed on 2010/03/26 10:53 UTC
Read the original article Hit count: 297

Filed under:
|
|
|
|

Possible Duplicate:
Can a C# thread really cache a value and ignore changes to that value on other threads?

Lets say we have this code:

bool KeepGoing = true;
DataInThread = new Thread(new ThreadStart(DataInThreadMethod));
DataInThread.Start();
//bla bla time goes on
KeepGoing = false;

private void DataInThreadMethod()
{
      while (KeepGoing)
      {
         //Do stuff
      }
   }
}

Now the idea is that using the boolean is a safe way to terminate the thread however because that boolean exists on the calling thread does that cause any issue?

That boolean is only used on the calling thread to stop the thread so its not like its being used elsewhere

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0