Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a windows form in a separate thread

I am dealing with running a control in a form, however the form itself is of no value to me. I essentially want the form to run a task and return a value, however for that I'd like to use something like an AutoResetEvent to return from the function call only when it has completed, which obviously would block the forms thread and make it impossible for the task to complete.

like image 976
Novikov Avatar asked Dec 10 '25 22:12

Novikov


1 Answers

I did this once for my project

var frmNewForm = new Form1();
var newThread = new System.Threading.Thread(frmNewFormThread);

newThread.SetApartmentState(System.Threading.ApartmentState.STA);
newThread.Start();

And add the following Method. Your newThread.Start will call this method.

public void frmNewFormThread()
{
    Application.Run(frmNewForm);
}
like image 112
franklins Avatar answered Dec 12 '25 14:12

franklins