Of this answer from SO.
You can try using a Invoke
call.
Example:
public static Form globalForm;
void Main()
{
globalForm = new Form();
globalForm.Show();
globalForm.Hide();
// Spawn threads here
}
void ThreadProc()
{
myForm form = new myForm();
globalForm.Invoke((MethodInvoker)delegate() {
form.Text = "my text";
form.Show();
});
}
The Invoke
call tells the form "Please, run this code on your thread instead of mine." Then you can make changes to the WinForms user interface from within the delegate.
For more information about Invoke
: link
You must use a WinForms object that already exists to call Invoke
. I have shown in the example how you can create a global object; Otherwise, if you have other Windows objects, you can use them.