PC Review


Reply
Thread Tools Rate Thread

a confusing problem relative to C# thread!

 
 
supermonkey
Guest
Posts: n/a
 
      24th Dec 2006
In my opinion, all subthreads will be terminated when the main thread exit
,but the following program pose a surprise to me~
After "Hello world!" was displayed on the screen, the program will wait
until the "RunMe called" was displayed!

why? is the subthread active though the main thread has exited?

how to explain this problem? Thank you :-)


// thread.cs
using System;
using System.Threading;

namespace ConsoleApplication10
{
class ThreadTest
{
public void RunMe()
{
Thread.Sleep(1000);
Console.WriteLine("RunMe called");
}

static void Main()
{
ThreadTest b = new ThreadTest();
//Thread t = new Thread(b.RunMe);

Thread t = new Thread(new ThreadStart(b.RunMe));
t.Start();
Console.WriteLine("Hello world!");
}
}
}


 
Reply With Quote
 
 
 
 
Stephany Young
Guest
Posts: n/a
 
      24th Dec 2006
The behaviour you are seeing is exactly the way it is designed to work.

In short, if you start a thread then you are responsible for stopping that
thread when it needs to be stopped.

There are exceptions to this rule of thumb and one of them is to make the
thread a background thread and then the behaviour you are expecting will
happen:

t.IsBackground = true;
t.Start();


"supermonkey" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> In my opinion, all subthreads will be terminated when the main thread exit
> ,but the following program pose a surprise to me~
> After "Hello world!" was displayed on the screen, the program will wait
> until the "RunMe called" was displayed!
>
> why? is the subthread active though the main thread has exited?
>
> how to explain this problem? Thank you :-)
>
>
> // thread.cs
> using System;
> using System.Threading;
>
> namespace ConsoleApplication10
> {
> class ThreadTest
> {
> public void RunMe()
> {
> Thread.Sleep(1000);
> Console.WriteLine("RunMe called");
> }
>
> static void Main()
> {
> ThreadTest b = new ThreadTest();
> //Thread t = new Thread(b.RunMe);
>
> Thread t = new Thread(new ThreadStart(b.RunMe));
> t.Start();
> Console.WriteLine("Hello world!");
> }
> }
> }
>
>



 
Reply With Quote
 
Duggi
Guest
Posts: n/a
 
      24th Dec 2006
Hi

A thread waits until all its child threads returns.( unless it is a
background thread).

So make RunMe thread as background thread. sothat main thread (Parent
thread ) will not wait for the background thread.

Thanks
-Srinivas.



supermonkey wrote:
> In my opinion, all subthreads will be terminated when the main thread exit
> ,but the following program pose a surprise to me~
> After "Hello world!" was displayed on the screen, the program will wait
> until the "RunMe called" was displayed!
>
> why? is the subthread active though the main thread has exited?
>
> how to explain this problem? Thank you :-)
>
>
> // thread.cs
> using System;
> using System.Threading;
>
> namespace ConsoleApplication10
> {
> class ThreadTest
> {
> public void RunMe()
> {
> Thread.Sleep(1000);
> Console.WriteLine("RunMe called");
> }
>
> static void Main()
> {
> ThreadTest b = new ThreadTest();
> //Thread t = new Thread(b.RunMe);
>
> Thread t = new Thread(new ThreadStart(b.RunMe));
> t.Start();
> Console.WriteLine("Hello world!");
> }
> }
> }


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      24th Dec 2006
Duggi <(E-Mail Removed)> wrote:
> A thread waits until all its child threads returns.( unless it is a
> background thread).


No, it's not quite like that. It's not to do with parent threads at all
- it's just that the *process* won't exit until all the non-background
threads have terminated.

You can create one thread (X) and then make X create several "child"
threads, and X can finish well before any of its children do - it won't
live on waiting for the children.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Duggi
Guest
Posts: n/a
 
      24th Dec 2006
Hi Jon,

You are right...

Verified with a sample code.

I apologize if I mislead anyone in the loop.

Thanks
-Srinivas.


Jon wrote:
> Duggi <(E-Mail Removed)> wrote:
> > A thread waits until all its child threads returns.( unless it is a
> > background thread).

>
> No, it's not quite like that. It's not to do with parent threads at all
> - it's just that the *process* won't exit until all the non-background
> threads have terminated.
>
> You can create one thread (X) and then make X create several "child"
> threads, and X can finish well before any of its children do - it won't
> live on waiting for the children.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very confusing problem!!! Simon Microsoft Outlook Installation 4 26th Nov 2008 08:46 AM
Confusing Problem Cincy Microsoft Excel Misc 2 13th Jul 2006 09:19 PM
Confusing Email Problem Robert P Microsoft Outlook 0 11th Jun 2004 03:02 PM
Thread problem: Access violation when Getting/Putting AppointmentItem's body in a thread. Dieter Verlaeckt Microsoft Outlook Program Addins 0 19th Mar 2004 09:36 AM
Confusing HardDisk Problem Dean DIY PC 1 6th Dec 2003 01:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 AM.