my winform has a thread running and I need it to notify main thread once it’s done, then the main thread can set progressbar back to normal state.
currently, what i have is something as following
------begin---------
// codes before....
call thread one....
create thread two instance then start thread two
---------end------------------
in thread two, i have
--------begin------
while(threadOne.isalive){
Thread.Sleep(500);
}
call setProgressbar delegate
---------end----------------
i did this instead of put an var in thread one is because thread one is recursive method….
i wonder if this correct way of doing it, can someone help?
thanks
EDIT: The answer below the line answers your explicit question, but if you’re using .NET 4, you should consider thinking at a higher level of abstraction instead – tasks. The Task Parallel Library exposes a richer set of functionality than “bare” threads, and you can do things like registering continuations (with
Task.ContinueWith) – and there you can even specify which task scheduler to use, so you can directly specify that when a background task has finished, a particular action should be invoked on the UI thread.Original answer
The simplest way would be to delegate the first thread’s responsibility, e.g.
If your thread might do different things, you could change your
WrapperStartto take the action to wrap:Then:
Note that if you do want the multi-thread solution, just use
Thread.Joininstead of looping explicitly.