473,545 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with thread.start...

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.
Nov 16 '05 #1
7 1501
VMI <vo******@yahoo .com> wrote:
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?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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 */

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
VMI <vo******@yahoo .com> wrote:
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?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
You have to call MessageBox.Show ("Program has ended");
at the end of your updateFiles procedure.

Willy.

"VMI" <vo******@yahoo .com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
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.

Nov 16 '05 #4
VMI <vo******@yahoo .com> wrote:
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(upd ateFiles);
}
else
{
threadJob = new ThreadStart(upd ateFiles2);
}
new Thread(threadJo b).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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
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.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com...
VMI <vo******@yahoo .com> wrote:
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(upd ateFiles);
}
else
{
threadJob = new ThreadStart(upd ateFiles2);
}
new Thread(threadJo b).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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
VMI <vo******@yahoo .com> wrote:
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/t...winforms.shtml for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
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 ProcessComplete dEventHandler() ;
private event ProcessComplete dEventHandler ProcessComplete d;
private void button1_Click(o bject sender, System.EventArg s e)
{
// bind the event to OnProcessComple ted
// This should preferably done within constructor of the current class
this.ProcessCom pleted += new ProcessComplete dEventHandler(O nProcessComplet ed);

Thread t = new Thread(new ThreadStart(Lon gRunningProcess ));
t.Start();
}

private void LongRunningProc ess()
{
// Long Running Process here

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

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

Hope this helps.
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.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2885
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am starting is that it has a welcome window first which gets displayed and then the real windows after a while,in other words it means that the...
9
6306
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use the attribute for the Main() function, I get "access denied error", if I use a ManagementEventWatcher to connect to the local machine to receive...
6
4967
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a...
3
1509
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be able to communicate with each other, either through a message loop, or some other manner. I ALSO need to be able to CALL specific functions / subs...
12
381
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am starting is that it has a welcome window first which gets displayed and then the real windows after a while,in other words it means that the...
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7749
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.