Reputation: 9090
I am getting an InvalidOperationException
with message Cross-thread operation not valid..
The _waitForm
is created in the constructor of the main form. The method in the screenshot gets called from another thread. I though this is what BeginInvoke
solves. I know that I am accessing the form from another thread than the one created.
Any ideas on how to solve this?
Here is the 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()
sorry for the screenshot, I wanted to show the whole picture
Upvotes: 2
Views: 1212
Reputation: 9090
I've found it. Of course it was my mistake!
The opposite of that method is the one showing the _waitForm
. I had mistakenly called _waitForm.Show()
from another than the UI thread without invokation. Strangely, this was able to succeed and caused this exception (that has the wrong message).
Upvotes: 0
Reputation: 8469
Have you tried to operate on _waitForm
through:
_waitForm.Invoke(new MethodInvoker(_waitForm.Hide));
Alternatively, if the above doesn't work:
_waitForm.Invoke(new MethodInvoker(() =>
{
_waitForm.Reset();
_waitForm.Hide();
}));
Upvotes: 3