I need to inform a so-called worker thread to stop work at the next available oppertunity. Currently I'm using something like this:
public void Run()
{
while (!StopRequested)
DoWork();
}
The concern I have is that StopRequested is being set to True on a different thread. Is this safe? I know I can't lock a boolean. Perhaps there's a different way to inform a thread that a stop is required. For example I would be happy to check Thread.CurrentThread.ThreadState == ThreadState.StopRequested
but it's not clear how I can set the thread state as such.