What's wrong with my cross-thread call in Windows Forms?

后端 未结 7 1901
南笙
南笙 2021-01-04 22:32

I encounter a problem with a Windows Forms application.

A form must be displayed from another thread. So in the form class, I have the following code:



        
7条回答
  •  执笔经年
    2021-01-04 23:09

    I also think SLaks is correct. From msdn (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx):

    If no appropriate handle can be found, the InvokeRequired method returns false.

    If its possible in your case, I would try to combine the creating and showing the control in a single method, i.e.:

    public DisplayDialog static Show()
    {
      var result = new DisplayDialog; //possibly cache instance of the dialog if needed, but this could be tricky
      result.ShowDialog(); 
      return result;
    }
    

    you can call Show from a different thread

提交回复
热议问题