473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ 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.Threadin g;

namespace ConsoleApplicat ion10
{
class ThreadTest
{
public void RunMe()
{
Thread.Sleep(10 00);
Console.WriteLi ne("RunMe called");
}

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

Thread t = new Thread(new ThreadStart(b.R unMe));
t.Start();
Console.WriteLi ne("Hello world!");
}
}
}
Dec 24 '06 #1
4 2092
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();
"supermonke y" <su*********@ma il.nankai.edu.c nwrote in message
news:%2******** ********@TK2MSF TNGP04.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.Threadin g;

namespace ConsoleApplicat ion10
{
class ThreadTest
{
public void RunMe()
{
Thread.Sleep(10 00);
Console.WriteLi ne("RunMe called");
}

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

Thread t = new Thread(new ThreadStart(b.R unMe));
t.Start();
Console.WriteLi ne("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.Threadin g;

namespace ConsoleApplicat ion10
{
class ThreadTest
{
public void RunMe()
{
Thread.Sleep(10 00);
Console.WriteLi ne("RunMe called");
}

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

Thread t = new Thread(new ThreadStart(b.R unMe));
t.Start();
Console.WriteLi ne("Hello world!");
}
}
}
Dec 24 '06 #3
Duggi <Du************ ***@gmail.comwr ote:
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.co m>
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.comwr ote:
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.co m>
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
2700
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 (testPrimes.py) with two algorithms that both check for primes by testing only odd numbers using factors up to the square root of the value, where Primes1 is based on all of the existing primes so far, and Primes2 is based on all odd numbers, I would...
3
19762
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 test case element1 and element2 should be placed side by side inside a centered white container element: http://www.markusernst.ch/test.htm
16
2060
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, sketching it out: <?php if (isset($_POST) && $_POST=="Submit") { ---Do a bunch of stuff--- if (isset($_SESSION)) unset($_SESSION);
8
1957
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 Access db, but to the local one. The OLEDBDATAAdapter wizard puts the entire path of the db (\\myserver\.... or c:\myprojects\....). The db resides within the site's folders; is there anyway to use relative paths for it (something like...
4
4177
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 os.path modules, but could not find any function of use. Should I write my own?
3
2294
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 stops or sleep until all other threads within the threadpool have all finished their jobs. what happens at the moment is that every other thread "should" just concatenate string data to public string variable when it finishes, but however the...
8
28516
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 way to have the location of the external text file be a relative, rather than absolute path (to be .\textfile.txt rather than C:\dir\textfile.txt)? Any advice that can be provided would be provided would be greatly appreciated/
11
1716
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 really appreciate it. I know I still need to tidy up my css a bit and shorten it but I don't want to that until its all right.
4
1825
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 read: http://mail.python.org/pipermail/python-list/2007-January/422973.html http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1e8dc93471a7079/8751c82cfe1ca3f2?lnk=gst&q=absolute+import#8751c82cfe1ca3f2...
0
8076
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8573
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8541
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8406
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5510
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2531
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
0
1389
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.