2012-12-17 45 views
2

我正在與消息Cross-thread operation not valid..出現InvalidOperationException內的BeginInvoke

_waitFormInvalidOperationException在主窗體的構造函數創建。截圖中的方法從另一個線程中調用。我雖然這是什麼BeginInvoke解決。我知道我是從另一個線程訪問窗體而不是創建窗體。 關於如何解決這個問題的任何想法?

enter image description here

這裏是堆棧跟蹤:

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

如果你想等待調用完成,你應該調用同步'Invoke()'。 – SLaks

+0

什麼是堆棧跟蹤? – SLaks

+0

你有兩個UI線程嗎? – SLaks

回答

3

您曾經試圖通過對_waitForm操作:

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

或者,如果上述不起作用:

_waitForm.Invoke(new MethodInvoker(() => 
{ 
    _waitForm.Reset(); 
    _waitForm.Hide(); 
})); 
+0

是的。你應該調用一個代碼來控制這個控件的調度器。 – lavrik

+0

這會導致死鎖。如果我使用Begin/End調用EndInvoke塊的執行 – Odys

0

我已經找到它了。當然,這是我的錯誤!

與該方法相反的是顯示_waitForm的那個。我從另一個UI線程錯誤地調用了_waitForm.Show()而沒有調用。奇怪的是,這是成功的,並導致這種異常(有錯誤的信息)。

相關問題