473,485 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

a confusing problem relative to C# thread!

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!");
}
}
}
Dec 24 '06 #1
4 2084
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" <su*********@mail.nankai.edu.cnwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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!");
}
}
}


Dec 24 '06 #2
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!");
}
}
}
Dec 24 '06 #3
Duggi <Du***************@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 24 '06 #4
Hi Jon,

You are right...

Verified with a sample code.

I apologize if I mislead anyone in the loop.

Thanks
-Srinivas.
Jon wrote:
Duggi <Du***************@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 24 '06 #5

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

Similar topics

9
2682
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
3
19745
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following...
16
2047
by: Shelly | last post by:
(posted previously on comp.lang.php but no response received. Cross-posted in the dreamweaver forum) I am confused about what goes on with method POST. Here is an overview of a my code,...
8
1947
by: dw | last post by:
Hello, all. I have a site that I'm testing on my LOCALHOST and also on a remote server, depending on whether I'm at work or at home. I don't want the OLEDBDATAAdapter to be pointing to the remote...
4
4164
by: Suresh Jeevanandam | last post by:
Hi, a = '/home/suresh/doc/html/a1/' b = '/home/suresh/doc/' I am looking for a standard function which will return the location of b relative to a i.e. '../..' I have gone through the os and...
3
2281
by: Maya Sam | last post by:
Hi all, I have the following code I created to do multiple websites data crawling using Asynchronous Thread calling it works fine however I'm confused when it comes to make the calling thread...
8
28495
by: ghadley_00 | last post by:
Hi, I have an access database with a table linked to an external text file. Occasionally the location of the database and the text file change (but are always in the same directory). Is there a...
11
1709
by: Ruth | last post by:
Remember this website? www.brianbarr.co.uk (please add /testsite/relative) You guys were helping me with it before. Bearing in mind my still newbieness, can anyone help with the following? I'd...
4
1804
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've...
0
7090
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,...
0
6960
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...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5418
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,...
0
4551
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...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
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 ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.