定义了一个全局集合
private List<Thread> _testThreads = new List<Thread>();
时钟中有如下一段代码
private void tmrTest_Tick(object sender, EventArgs e)
{     for (int i = 0; i < _testThreads.Count; i++)//如果定时器到时,但做通讯测试的线程还没有结束
     {
         if (_testThreads[i].IsAlive)
         {
             _testThreads[i].Join();
         }
     }     Thread.Sleep(350);    //用多线程进行通讯测试
     for (int index = 0; index < _seiPcbas.Count; index++)
    {
        _seiPcbas[index].SetTestCondition(cmbCommand.Text, _repeatTimes, _repeatInterval, 
            new CallBackDelegate(CallBackRecord));
        Thread threadTest = new Thread(new ThreadStart(_seiPcbas[index].TestReadCommand));
        _testThreads.Add(threadTest);
        threadTest.Start();
     }
}想在点击"停止"按钮时,首先停止时钟,然后终止线程或等待线程结束,但用Join不动了,用Abort又除出现
如下错误:
未处理 System.ObjectDisposedException
  Message="已关闭 Safe handle"
  Source="mscorlib"
  ObjectName=""
  StackTrace:
       在 Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
       在 System.Threading.EventWaitHandle.Set()
       在 System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOverlapped)
       在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)请高手指教

解决方案 »

  1.   

    从你的抛出异常可以知道,是由于资源已经析构了,但是线程还在继续使用已经析构的资源
    你在Timer中起线程,而且起了多个线程,不明白你要干啥???这样的写法挺奇怪的,希望能说明你要干什么
      

  2.   

    _testThreads[i].Join(); 会让主线程阻塞,直到线程_testThreads[i]执行结束,所以你用join不动了很正常。Thread.Sleep(350); 是多余的,因为如果循环被执行,主线程已经阻塞。主线程要让它自然结束,强行Abort肯定会报错的。