The reason we want to have a look at a background thread specifically is because by default, all threads created by the main app thread or Thread
class constructor are foreground threads. So, what exactly separates a foreground thread from a background thread? Well, background threads are identical to foreground threads with the exception that if all foreground threads are terminated, the background threads are stopped too. This is useful if you have a process in your application that must not stop the application from terminating. In other words, while your application is running, the background thread must continue to run.
We will create a simple application that defines the thread created as a background thread. It will then suspend, resume, and abort the thread.
Chapter7
:Class1.cs,
which we renamed to Recipes.cs
in order to distinguish the code properly. You can, however, rename your class to whatever you like if that makes more sense to you.Chapter7
library project:namespace Chapter7 { public class Recipes { } }
Recipes
class, add a method called DoBackgroundTask()
with the public void
modifiers, and add the following console output to it:public void DoBackgroundTask() { WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} has a threadstate of {Thread.CurrentThread.ThreadState} with {Thread.CurrentThread.Priority} priority"); WriteLine($"Start thread sleep at {DateTime.Now.Second} seconds"); Thread.Sleep(3000); WriteLine($"End thread sleep at {DateTime.Now.Second} seconds"); }
CodeSamples
, added previously, add a reference to the Chapter7
class library by right-clicking on References under the CodeSamples
project and selecting Add Reference from the context menu:Chapter7
solution by going to Projects | Solutions. This will allow you to use the classes we just created in your console application:void Main
method, create a new instance of your Recipes
class and add it to a new thread called backgroundThread
. Define this newly created thread to be a background thread and then start it. Finally, set the thread to sleep for five seconds. We need to do this because we have created a background thread that is set to sleep for three seconds. Background threads do not prohibit foreground threads from terminating. Therefore, if the main application thread (which is by default a foreground thread) terminates before the background thread completes, the application will terminate and also terminate the background thread:static void Main(string[] args) { Chapter7.Recipes oRecipe = new Chapter7.Recipes(); var backgroundThread = new Thread(oRecipe.DoBackgroundTask); backgroundThread.IsBackground = true; backgroundThread.Start(); Thread.Sleep(5000); }
backgroundThread.Priority = ThreadPriority.Lowest;
. This line will downgrade the thread priority:Chapter7.Recipes oRecipe = new Chapter7.Recipes(); var backgroundThread = new Thread(oRecipe.DoBackgroundTask); backgroundThread.IsBackground = true; backgroundThread.Priority = ThreadPriority.Lowest; backgroundThread.Start(); Thread.Sleep(5000);
DoBackgroundTask()
method and add Thread.CurrentThread.Abort();
right before Thread.Sleep(3000);
is called. This line will prematurely kill the background thread. Your code should look like this:public void DoBackgroundTask() { WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} has a threadstate of {Thread.CurrentThread.ThreadState} with {Thread.CurrentThread.Priority} priority"); WriteLine($"Start thread sleep at {DateTime.Now.Second} seconds"); Thread.CurrentThread.Abort(); Thread.Sleep(3000); WriteLine($"End thread sleep at {DateTime.Now.Second} seconds"); }
Thread.Sleep
method is called. Aborting a thread in this way, however, is generally not recommended:Being able to create a background thread is a great way to work on a different thread from the main thread while not interfering with the process of the main application thread. Another added benefit is that the background thread is terminated as soon as the main application thread is completed. This process ensures that your application will terminate gracefully.