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

后端 未结 7 1904
南笙
南笙 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:11

    You could always try testing against a different control.

    For example, you could access Application.Forms collections

    public Control GetControlToInvokeAgainst()
    {
        if(Application.Forms.Count > 0)
        {
            return Application.Forms[0];
        }
        return null;
    }
    

    Then in your DisplayDialog() method, call the GetControlToInvokeAgainst() and test for null before trying to perform an invokerequired call.

提交回复
热议问题