santBart
santBart

Reputation: 2496

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?

Upvotes: 3

Views: 2017

Answers (2)

Timmerz
Timmerz

Reputation: 6199

as flq mentioned you can use thread.join, that will allow the thread to finish whatever it is doing and you can continue after that point...however since you are working on a forms application you'll probably be blocking the ui thread if you join from there...

you should try a different pattern for fixing this instead of blocking...use delegates or events to signal when something has finished...also known as asynchronous processing.

Upvotes: -1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174447

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();

Upvotes: 5

Related Questions