尝试重新启动线程时,我不时会收到System.Threading.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); }
所以有两个问题 – 这是正确的做事方式,而且,是否有办法防止错误发生?