Wednesday, April 18, 2012

C# alternative to Thread.Suspend() method

Thread.Suspend() method is obsolete as you know. I want to suspend thread immidiately when button click event comes. I used Thread.Suspend() and it works perfect but everyone suggest that using Thread.Suspend() method is not a good method to suspend the task. I used a flag to suspend the task but every time when a button click event comes, i have to wait to exit from task. I used Thread.IsAlive flag to wait the thread to exit but this method freezes form.



void ButtonClickEvent(object sender, ButtonClickEventArgs e)
{
TheadExitFlag = false;

if(MyThread != null)
{
while(MyThread.IsAlive);
//MyThread.Suspend();
}
}

void MyTask(void)
{
while(TheadExitFlag)
{
// some process
Thread.Sleep(5000);
}
}


How can i suspend the thread immidiately?





No comments:

Post a Comment