I Need Generate GUI for A winform In Thread1 By Result Of Thread2 !
But Thread1 Start From Thread2.
If Result Of Thread2 Equal 1,I Need To Show a Grid on My winfrom, Else Show A Tab Control On My Winform.
I Need Generate GUI for A winform In Thread1 By Result Of Thread2 !
But Thread1 Start From Thread2.
If Result Of Thread2 Equal 1,I Need To Show a Grid on My winfrom, Else Show A Tab Control On My Winform.
The UI thread must build it or atleast add it to the form.
But in Thread 2 you can collect all required information or maybe controls and pass them by invoke or as BackgroundWorker result to the UI thread.
You should only keep a single UI thread, and never create any UI elements in another thread.
Microsoft has many articles on this topic and demonstrates the correct way to handle async operations, such as
If I understood you correctly there is a need of creating GUI elements in one thread for later using them in GUI thread.
And as far as I remember it is not possible because control remembers the thread it was created on and later checks whether calls are made within that thread.
I'd recommend you to generate data only in background thread and pass it to gui thread where you can bind this data or generate ui elements to represent it. Moreover it conforms to the best practice of separation of concerns.
Lex Li has posted the link in comments to the question devoted to passing data to GUI thread.
I am not sure, I can understand your problem correctly but here is the code to create UI controls in a different thread.
var th = new Thread(() =>
{
//A sample form with a RichTextBox control.
var f = new Form();
f.Controls.Add(new RichTextBox() { Dock = DockStyle.Fill });
Application.Run(f);
});
th.SetApartmentState(ApartmentState.STA);
th.Start();