我收到InvalidOperationException
消息Cross-thread operation not valid..
_waitForm
是在主窗体的构造函数中创建的。屏幕截图中的方法从另一个线程调用。我虽然这是BeginInvoke
解决的问题。我知道我从另一个线程访问该表单而不是创建的表单。
关于如何解决这个问题的任何想法?
这是stacktrace:
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()
对不起截图,我想展示整个图片
答案 0 :(得分:3)
您是否曾尝试通过以下_waitForm
进行操作:
_waitForm.Invoke(new MethodInvoker(_waitForm.Hide));
或者,如果以上情况不起作用:
_waitForm.Invoke(new MethodInvoker(() =>
{
_waitForm.Reset();
_waitForm.Hide();
}));
答案 1 :(得分:0)
我找到了。当然是我的错!
与该方法相反的是显示_waitForm
的方法。在没有调用的情况下,我错误地从另一个UI线程调用了_waitForm.Show()
。奇怪的是,这能够成功并导致这种异常(这有错误的信息)。