up
函數如下(form在外部已分配)
private void MsgThread()
{
while(true)
{
if(ShowWindow)
{
form.Show();
ShowWindow = false;
}
Thread.Sleep(20);
}
}

解决方案 »

  1.   

    我只看到:while(true)
    {
       Thread.Sleep(20);
    }
      

  2.   

    To FallingAngle (海洲):WinForm中的控件在跨线程时需要非常谨慎,因为WinForm中的GUI控件并不是“线程安全”的。以下是MSDN中的一段叙述:******************************************************
    Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control. There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
    ******************************************************因此,当跨线程时,需要随时留意控件的线程安全问题。程序的很多看似匪夷所思的行为往往皆由跨线程导致。一些“重量级”控件尤其如此,如DataGrid等。针对您的具体问题,需要使用Invoke()函数来解决。以下是针对您的具体问题的一段使用Invoke()的例子代码:public class Form1 : System.Windows.Forms.Form
    {
    private bool ShowWindow=false;
    private System.Windows.Forms.Form form=null; delegate void MyDelegate();
    MyDelegate myDelegate=null; private void menuItem2_Click(object sender, System.EventArgs e)
    {
    form=new Form();
    System.Threading.Thread thread=new Thread(new ThreadStart(this.MsgThread));
    thread.Start();
    myDelegate=new MyDelegate(form.Show);
    } private void MsgThread()
    {
    while(true)
    {
    if(ShowWindow)
    {
    this.Invoke(this.myDelegate);
    ShowWindow = false;
    break;
    }
    Thread.Sleep(20);
    }
    }
    }希望以上所述能够对您有所帮助,同时感谢您关系和使用微软的技术和产品。-  微软全球技术中心  VC技术支持 
     
    本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。  
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。  
      

  3.   

    To FallingAngle (海洲):WinForm中的控件在跨线程时需要非常谨慎,因为WinForm中的GUI控件并不是“线程安全”的。以下是MSDN中的一段叙述:******************************************************
    Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control. There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
    ******************************************************因此,当跨线程时,需要随时留意控件的线程安全问题。程序的很多看似匪夷所思的行为往往皆由跨线程导致。一些“重量级”控件尤其如此,如DataGrid等。针对您的具体问题,需要使用Invoke()函数来解决。以下是针对您的具体问题的一段使用Invoke()的例子代码:public class Form1 : System.Windows.Forms.Form
    {
    private bool ShowWindow=false;
    private System.Windows.Forms.Form form=null; delegate void MyDelegate();
    MyDelegate myDelegate=null; private void menuItem2_Click(object sender, System.EventArgs e)
    {
    form=new Form();
    System.Threading.Thread thread=new Thread(new ThreadStart(this.MsgThread));
    thread.Start();
    myDelegate=new MyDelegate(form.Show);
    } private void MsgThread()
    {
    while(true)
    {
    if(ShowWindow)
    {
    this.Invoke(this.myDelegate);
    ShowWindow = false;
    break;
    }
    Thread.Sleep(20);
    }
    }
    }希望以上所述能够对您有所帮助,同时感谢您关系和使用微软的技术和产品。-  微软全球技术中心  VC技术支持 
     
    本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。  
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。