0

Can some one explain why this is happening? In the following code, even though the condition is returning true, the code in the if condition is not executed.

//suspend thread 
if (objThread.ThreadState == ThreadState.Running)
{
    objThread.Suspend();
}

Okay so this my scenario. I have two TabItems. In Window_Loaded event, I start a thread which updates values to a DataGrid continuously. Now when I select the other TabItem, I want the updating in the first TabItem to stop. So, I put the above code in TabItem_LostFocus event. The thread should be suspended when the focus is lost, right? In the UserControl_Loaded event, I am checking for the state of the thread.

if (objThread.ThreadState == ThreadState.Suspended)
{
    objThread.Resume();
}
else
{
    // Start the thread
    objThread.Start();
}

But, I get an Error like this: "Thread is running or terminated; it cannot restart."

But the thread is alive, because my UI is still being updated.

Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • 2
    How did you verify this? The condition may very well be `true` when you test it, then subsequently `false` by the time you reach the `if` statement. – Frédéric Hamidi Jun 16 '14 at 10:33
  • 8
    The number of times you should use `Thread.Suspend` is **staggeringly** small; the warning that appears in your build output is not just for giggles: `[ObsoleteAttribute("Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202", false)]` – Marc Gravell Jun 16 '14 at 10:34
  • `Thread.Suspend()` method is obsolete. You can use [`AutoResetEvent`](http://msdn.microsoft.com/ru-ru/library/system.threading.autoresetevent(v=vs.110).aspx), [Thread.Join](http://msdn.microsoft.com/ru-ru/library/23f7b1ct(v=vs.110).aspx) or just `Thread.Sleep()`. – Alex Skiba Jun 16 '14 at 10:35
  • @FrédéricHamidi: I receive data over the network continuously. So, to simulate the data receiving, I commented out the actual code and add a variable which is incremented in a loop. So, the thread is always running right? – Abhishek Jun 16 '14 at 10:36
  • @MarcGravell: Yes, I did read the warning in the build output. It is only for testing the functionality that I used `Thread.Suspend()`. It's going be different later. But, I wanted to know why. – Abhishek Jun 16 '14 at 10:39
  • 2
    if the `if` condition resolves true, then the code inside the `if` block is going to be executed. Frankly, I suspect that this is actually a timing issue, and it *isn't* `true` at the point of being evaluated - **or**, there's an outside chance that the project hasn't successfully built since adding the code, and you are actually debugging against an old version (it does happen occasionally) – Marc Gravell Jun 16 '14 at 10:48
  • You've discovered what's commonly known as a [race condition](http://stackoverflow.com/questions/34510/what-is-a-race-condition). – James Jun 16 '14 at 10:49

0 Answers0