Waiting for a thread to finish? [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Okay - this is what my code is trying to accomplish (in relative pseudo-code):

Code:
SetStatusBar("Start Operation 1");
SetLabel("Start Operation 1");
Thread th1 = new Thread (new ThreadStart(Operation1));
th1.Start();
// WAIT FOR th1 to finish without loosing focus on the UI
SetStatusBar("Operation 1 Finished");
SetLabel("Operation 1 Finished");

SetStatusBar("Start Operation 2");
SetLabel("Start Operation 2");
Thread th2 = new Thread (new ThreadStart(Operation2));
th2.Start();
// WAIT FOR th2 to finish without loosing focus on the UI
SetStatusBar("Operation 2 Finished");
SetLabel("Operation 2 Finished");

SetStatusBar("Start Operation 3");
SetLabel("Start Operation 3");
Thread th3 = new Thread (new ThreadStart(Operation3));
th3.Start();
// WAIT FOR th3 to finish without loosing focus on the UI
SetStatusBar("Operation 3 Finished");
SetLabel("Operation 3 Finished");

btnCanCloseWindow.Enabled = true;	// this can only be true if all the tasks have run

That is pretty much a dumb'ed down idea of how my application works... (where the thread functions OperationX will launch corresponding processes). As it is this works fine except that I am unable to find a way to WAIT for a thread to complete without holding the form itself hostage.

I tried using "thX.Join()" but this will block the UI thread until it returns (which is not ok)... while each operation is running I need the user to be able to move the window (UI) if he wants to, minimize it, or simply stare at it to see what is being done by looking at the status bar or label... But it can't look out of focus or as if it has frozen...

Any help would be much appreciated.
Thanks,
 
I haven't tried this but maybe this can help. And sorry this is in VB but hopefully it will translate ok for C#

Code:
 Do
      Application.DoEvents() 'I am not sure what the equilevant is for c#
 Loop Until th1.ThreadState = Threading.ThreadState.Stopped

Hmm, I definetely need to check this out as I was looking for something similar.:D
 
That doesn't seem to solve the problem - using that code (in C# I used a WHILE loop instead) the UI was still held hostage by the threads as they were working (it gets auto-hidden and if I force a .Show() it is out of focus and looks like it is frozen).

Any other ideas?
I assume one of the only options would be event driven?
Or using the WaitOne() type functionality?
 
There is rarely a situation where a DoEvents loop is your best solution, especially with threading.

Shaitan, why don't use you use a BackgroundWorker component. If you want to do something when the operation is complete, you can use the WorkCompleted event of the BackgroundWorker. This method would leave your UI thread completely free to go about business as usual.

[Edit]Just saw a post you wrote saying you are using dotnet 1.x... oh well, sorry. You could write a method that would be invoked on the UI thread from a background thread when the operation is finished.[/edit]
 
Last edited:
Ok - if I chane my architecture around a little where I no longer care in between each thread... so I decided to go with something like this:

Code:
{
Thread th = new Thread(ThreadStart(Operations));
th.Start();
}

private void Operations()
{
SetStatusBar("Start Operation 1");
SetLabel("Start Operation 1");
LaunchOperation(1);
SetStatusBar("Operation 1 Finished");
SetLabel("Operation 1 Finished");

SetStatusBar("Start Operation 2");
SetLabel("Start Operation 2");
LaunchOperation(2);
SetStatusBar("Operation 2 Finished");
SetLabel("Operation 2 Finished");

SetStatusBar("Start Operation 3");
SetLabel("Start Operation 3");
LaunchOperation(3);
SetStatusBar("Operation 3 Finished");
SetLabel("Operation 3 Finished");
}

Now - this works fine for me except for the last most important thing....
"btnCanCloseWindow.Enabled = true; // this can only be true if all the tasks have run"

Can I somehow have the thread "th" fire off an EVENT or something that would be caught by the UI where I could add that code? I know this is do-able as a DELEGATE function but thought it would be more appropirate as an EVENT...

Any clues?
 
Back
Top