Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# how to make thread wait

Form form1 = new Form();
Thread newThread = new Thread(() => 
        form = form1
);
newThread.Start();
while(form == null)
{
     WAIT? 
}

Can anyone help me how to make thread wait befor going on with execution? And not to use thread.sleep?

like image 690
santBart Avatar asked Aug 17 '26 02:08

santBart


1 Answers

You can use a ManualResetEvent:

var mre = new ManualResetEvent(false);
Form form1 = new Form();
Thread newThread = new Thread(() => {
        form = form1;
        mre.Set();
});
newThread.Start();
mre.WaitOne();
like image 161
Daniel Hilgarth Avatar answered Aug 18 '26 17:08

Daniel Hilgarth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!