Control.Invoke失败,但IsHandleCreated为true

时间:2016-02-01 17:10:06

标签: c# winforms invoke

使用旧的Invoke消息

,对InvalidOperationException的调用失败
 "Invoke or BeginInvoke cannot be called on a control until the window
 handle has been created"

但我们正在做我们认为必须做的事情:

public class MyControl : UserControl 
{

    private void TaskWhenABackgroundThreadFinishes(int arg1, int arg2) 
    {
        try
        {
            if (!IsDisposed && !Disposing && IsHandleCreated)
            {
                if (InvokeRequired)
                {
                    Invoke(new AsyncCallbackDelegate(TaskWhenABackgroundThreadFinishes), 
                        arg1, 
                        arg2);
                }
                else
                {
                    // Perform the task with arg1 and arg2
                }
            }
        }
        catch (InvalidOperationException ode)
        {
            // Log the exception
        }
   }
}

我们怎样才能得到这个结果?我们正在检查控件是否已经处理,没有处理,并且有一个Windows句柄,为什么消息说我们必须有句柄?

在我们的客户机器上经常发生这个问题,而不是我们的开发机器,所以我们无法确定我们是否已经确定了问题。

我们应该做的其他事情吗?

这可能是一场比赛,世界在IsDisposed,Disposing和IsHandleCreated的测试之间发生变化,然后最终调用Invoke?

0 个答案:

没有答案