如果需要太多时间,我有一个Timer
必须取消的。Thread
System.Timers.Timer timer_timers = new System.Timers.Timer();
Thread thread = new Thread(startJob);
thread.Name = "VICTIM_THREAD";
启动Thread
方法时,我启动Timer
并将当前线程作为参数传递给事件。
public void startJob()
{
Debug.WriteLine("Name: " + Thread.CurrentThread.Name);
timer_timers.Elapsed += (sender, e) => T_Elapsed(sender, e, Thread.CurrentThread);
timer_timers.Interval = 5000;
// Start simulation process
while (true)
{
Thread.Sleep(700);
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name + " ALIVE: " + thread.IsAlive);
}
}
定时器事件:
private void T_Elapsed(object sender, ElapsedEventArgs e, Thread currentThread)
{
// EDIT: show the correct NAME! of the thread
Debug.WriteLine("Name: " + currentThread.Name);
System.Timers.Timer tim = sender as System.Timers.Timer;
currentThread.Abort(); // <-- this line throws exception
if (tim != null)
{
tim.Stop();
}
}
但是这个Abort
电话给我抛出了一个异常:
'无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部'
并且线程仍然活着。如果我在 之前启动计时器startJob()
并直接传递线程它工作正常。
public void startThread()
{
timer_timers.Elapsed += (sender, e) => T_Elapsed(sender, e, thread);
timer_timers.Interval = 5000;
timer_timers.Start();
thread.Start();
}
public void startJob()
{
// Start simulation process
while (true)
{
Thread.Sleep(700);
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name + " ALIVE: " + thread.IsAlive);
}
}
问题:为什么Thread.CurrentThread
版本不工作?是因为我还必须中止计时器线程吗?我在这里想念什么?
我发现这个例外的答案来自不同的上下文,并不能真正帮助我理解为什么。
编辑:我知道这是中止或取消线程的错误方法。它应该做的工作是打开一个 SerialPort。但是每隔约 200 次,线程就永远不会返回,我需要杀死它,不管后果。模拟while循环可能是一个不好的例子。