代码中的thread.sleep()是让那个线程等待,如果只是让包含该语句的当前线程等待,那么为什么我的一个线程用thread.sleep()另一个线程也在等待?
while (FrmMain.setposIsOk == true)
                {
                    if ( ComMod.isHaveOk == true)
                    {
                        this.Close();
                    }
                    else
                    {
                        Thread.Sleep(5);
                    }
                }
就像这段代码,当线程“FrmMain.setposIsOk == true”完成后等待“ComMod.isHaveOk == true”线程完成,可为什么会进入死循环。就是说if ( ComMod.isHaveOk == true)一直不成立。

解决方案 »

  1.   

    应该和你的两个线程关系的,再者,等待两个线程都结束最好不要用这种方式,.net framework都给出了其它的方法
      

  2.   

    在 if ( ComMod.isHaveOk == true)和另一个线程内断点一下,另外一个线程应该不会等待。
      

  3.   

    这个结构看上去可不那么友好啊,这个线程的目的是关闭主窗体?那应该使用安全代码调用//同步等待机制
    AutoResetEvent resetEvent = new AutoResetEvent();private void CloseWindow()
    {
        resetEvent.WaitOne();
        this.Close();
    }private delegate void CloseWindowCallBack();private void YourThreadStartEvent()
    {
    while (FrmMain.setposIsOk == true) 
                    { 
                        if ( ComMod.isHaveOk == true) 
                        { 
                            FrmMain.setposIsOk = false;
                            CloseWindowCallBack closeWindow = new CloseWindowCallBack(CloseWindow);
                            this.Invoke(closeWindow);
                        } 
                        else 
                        { 
                            Thread.Sleep(5); 
                        } 
                    } 
          resetEvent.Set();
    }