Help with thread.start...

V

VMI

In the following code, how can I display the MessageBox once MyThread has
finished executing? updateFiles method copies 5 humongous files to the HDD
but for some reason, the MessageBox is displayed before updateFles has
finished executing.

This is the code:

MyThread = new Thread(new ThreadStart (updateFiles));
MyThread.Start();
MessageBox.Show("Program has ended");


private void updateFiles()
{
/* several File.Copy calls of five 400MB files */
}

Thanks.
 
J

Jon Skeet [C# MVP]

VMI said:
In the following code, how can I display the MessageBox once MyThread has
finished executing? updateFiles method copies 5 humongous files to the HDD
but for some reason, the MessageBox is displayed before updateFles has
finished executing.

Of course - precisely because you're doing it in a different thread!

Why not just put the message box at the end of the updateFiles method?
 
V

VMI

Because this method is conditioned by an If statement. And there are several
lines of code that run after the Threads regardless of what thread is
executed. The code's like this:
Thread MyThread = null;
if (execute)
{
MyThread = new Thread(new ThreadStart (updateFiles));
MyThread.Start();
}
else
{
MyThread = new Thread(new ThreadStart (updateFiles2));
MyThread.Start();
}

/* Lines of code that are run after files are copied */
 
W

Willy Denoyette [MVP]

You have to call MessageBox.Show("Program has ended");
at the end of your updateFiles procedure.

Willy.
 
J

Jon Skeet [C# MVP]

VMI said:
Because this method is conditioned by an If statement. And there are several
lines of code that run after the Threads regardless of what thread is
executed. The code's like this:
Thread MyThread = null;
if (execute)
{
MyThread = new Thread(new ThreadStart (updateFiles));
MyThread.Start();
}
else
{
MyThread = new Thread(new ThreadStart (updateFiles2));
MyThread.Start();
}

/* Lines of code that are run after files are copied */

It sounds like you actually want:

ThreadStart threadJob;

if (execute)
{
threadJob = new ThreadStart(updateFiles);
}
else
{
threadJob = new ThreadStart(updateFiles2);
}
new Thread(threadJob).Start();

However, either way, those "lines of code that are run after files are
copied" will be executed pretty much immediately, while the files are
being copied. If you didn't want it to happen in the background, why
bother starting it in a different thread in the first place? You could
call Thread.Join, but there seems to be very little point in starting a
thread only to join it immediately.
 
V

VMI

I did it with threads because, once the files start being copied, the
application window looks like it's frozen. So the user might think the
program froze when it's actually copying one of the really huge files. I
want the user to be able to move the window around while the files are being
copied.
 
J

Jon Skeet [C# MVP]

VMI said:
I did it with threads because, once the files start being copied, the
application window looks like it's frozen. So the user might think the
program froze when it's actually copying one of the really huge files. I
want the user to be able to move the window around while the files are being
copied.

Right. So you do indeed need a new thread, but you can't just put code
after the Thread.Start call. You should make your copying methods tell
the UI thread when they're finished.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml for more
information.
 
H

Hayato Iriumi

Hello VMI,

You should use event to notify the caller of the completion of process. I'm
going to assume that updateFiles procedure exists within the same class.
Here is the sample.

private delegate void ProcessCompletedEventHandler();
private event ProcessCompletedEventHandler ProcessCompleted;
private void button1_Click(object sender, System.EventArgs e)
{
// bind the event to OnProcessCompleted
// This should preferably done within constructor of the current class
this.ProcessCompleted += new ProcessCompletedEventHandler(OnProcessCompleted);

Thread t = new Thread(new ThreadStart(LongRunningProcess));
t.Start();
}

private void LongRunningProcess()
{
// Long Running Process here

// Process finished, now raise the event.
ProcessCompleted();
}

private void OnProcessCompleted()
{
MessageBox.Show("Process Completed");
}

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top