I was trying to stop some threads, read some things about the proper way to do it gracefully, but I must be doind something wrong because it simply doesn't work. At first I tried without the lock() with _IsRunning being volatile, then tried with the locks. Here is what I've got.
private volatile bool _IsRunning;
private static readonly object runLock = new object();
public void Start()
{
if (_IsRunning == true)
return;
_IsRunning = true;
(new System.Threading.Thread(new System.Threading.ThreadStart(SendLoop))).Start();
}
public void Stop()
{
lock (runLock)
{
_IsRunning = false;
}
}
private void SendLoop()
{
while (_IsRunning)
{
lock (runLock)
{
if (_sockets.Count > 0)
{
//some stuff
}
else
{
System.Threading.Thread.Sleep(10);
}
}
}
}
I set a breakpoint at my while(), and _IsRunnig is still true even though I passed in Stop().