F
Frank Rizzo
Hello,
I want to use a variable as a signal to the thread that it should stop
what it's doing (code below). As a rule, should I lock an object only
when I am writing to it or do I have to lock on reading as well? For
instance, consider the following code:
//running in a non-GUI thread
foreach (object o in objects)
{
...
if (SignalObject.StopSignalDetected) break;
}
//running in the GUI thread.
void btnStop_Click(...)
{
lock (SignalObject)
{
SignalObject.StopSignalDetected = true;
}
}
Is SignalObject.StopSignalDetected safe here? Or should I lock it in
the non-GUI thread as well, even though it will only read it?
Thanks
I want to use a variable as a signal to the thread that it should stop
what it's doing (code below). As a rule, should I lock an object only
when I am writing to it or do I have to lock on reading as well? For
instance, consider the following code:
//running in a non-GUI thread
foreach (object o in objects)
{
...
if (SignalObject.StopSignalDetected) break;
}
//running in the GUI thread.
void btnStop_Click(...)
{
lock (SignalObject)
{
SignalObject.StopSignalDetected = true;
}
}
Is SignalObject.StopSignalDetected safe here? Or should I lock it in
the non-GUI thread as well, even though it will only read it?
Thanks