Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Thread Creation Question

Tags:

c#

.net

When creating a new System.Thread instance the default value of IsBackground is false. Can anybody shed the light on why this would be the default value would be not be true. My inclination, correct or not, when creating a thread is that it would be run in the background of the main thread.

Thank you for the quick responses.

I do provide functionality in the destructor of my component to allow for the thread to end gracefullly. I am using a ManualResetEvent and Join.

I fully understand that allowing the thread to end gracefully is correct and proper. What I am not conceptualizing is that why. by default, a thread must hang an entire application if it does not end successfully when the application is exiting.

like image 449
gooch Avatar asked Nov 10 '09 23:11

gooch


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

IsBackground means that the thread is terminated when the application terminates. This is rarely desirable behaviour, because it means that thread can't stop and clean up properly.

Instead, the app should signal the thread to terminate, wait until it has done so, and then close properly.

That's my summary of this blurb from the Thread.IsBackground MSDN article:

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.

like image 152
Neil Barnwell Avatar answered Sep 22 '22 19:09

Neil Barnwell


This information from the MSDN page on IsBackground explains the difference between background and foreground threads.

A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.

like image 20
ChrisF Avatar answered Sep 22 '22 19:09

ChrisF