I have a main UI thread which runs the application and creates the main window form (let's call it W
). I have also a secondary thread that I spin up and which creates a dialog box (let's call it B
).
I want to set the owner of the dialog B
to be the main window W
. The setting of B
s owner happens on the thread that created B
. Basically:
b.Owner = w;
but this throws a cross-thread exception telling me that I am tryng to access the W
object from the wrong thread.
So I tried to execute the code on the main UI thread, by using a Control.Invoke
on W
. But then, I get the same error telling me that I am trying to access B
from the wrong thread:
System.InvalidOperationException was unhandled by user code
Message=Cross-thread operation not valid: Control 'B' accessed from a
thread other than the thread it was created on.
Source=System.Windows.Forms
How am I supposed to do it right?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…