InvalidOperationException

InvalidOperationException

我收到带有消息InvalidOperationExceptionCross-thread operation not valid.._waitForm在主窗体的构造函数中创建。屏幕截图中的方法是从另一个线程调用的。我虽然是BeginInvoke解决的。我知道我正在从另一个线程(而不是创建的线程)访问表单。
关于如何解决这个问题的任何想法?

这是堆栈跟踪:

   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.get_ContainsFocus()
   at System.Windows.Forms.Control.SelectNextIfFocused()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Hide()
   at YYYYYY.Boundary.ZzzzzForm.<HideWaitForm>b__c() in R:\Projects\XXXX\trunk\src\YYYYYY\Boundary\ZzzzzForm.cs:line 514
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()

抱歉,我要显示整个图片

最佳答案

您是否尝试过通过以下方式对_waitForm进行操作:

_waitForm.Invoke(new MethodInvoker(_waitForm.Hide));

或者,如果上述方法不起作用:
_waitForm.Invoke(new MethodInvoker(() =>
{
    _waitForm.Reset();
    _waitForm.Hide();
}));

关于c# - BeginInvoke中的InvalidOperationException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13918821/

10-11 20:23