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 TabItem
s. 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.