嘗試重新啓動線程時,我經常會遇到System.Threading.ThreadStateException。有問題的代碼如下:嘗試重新啓動線程時發生ThreadStateException
// Make sure the thread is done stopping
while (this.mThread.ThreadState == ThreadState.Running)
{
Thread.Sleep(0);
}
// Respawn a thread if the current one is stopped or doesn't exist
if (this.mThread == null || this.mThread.ThreadState == ThreadState.Stopped)
{
this.mThread = new Thread(new ParameterizedThreadStart(Monitor)); }
// Start the thread
if (check)
{
this.mThread.Start(60000);
}
else
{
this.mThread.Start(0);
}
所以兩個問題 - 這是做事的正確方法,它是,有沒有辦法來防止錯誤的發生?