How to do it...

  1. Create a new console application in Visual Studio.
  1. Next, add a class called Demo to your console application.
  1. Inside the Demo 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");
}
Make sure that you have added the using statements for System.Threading and static System.Console to your using statements.
  1. In the void Main method of your console application, create a new instance of your Demo 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 5 seconds. We need to do this because we created a background thread that is set to sleep for 3 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)
{
Demo oRecipe = new Demo();
var backgroundThread = new Thread(oRecipe.DoBackgroundTask);
backgroundThread.IsBackground = true;
backgroundThread.Start();
Thread.Sleep(5000);
}
  1. Run your console application by pressing F5. You will see that we have created a background thread with a normal priority:
  1. Let's modify our thread and set its priority down to low. Add this line of code to your console application:
        backgroundThread.Priority = ThreadPriority.Lowest;

This line will downgrade the thread priority:

        Demo oRecipe = new Demo();
var backgroundThread = new Thread(oRecipe.DoBackgroundTask);
backgroundThread.IsBackground = true;
backgroundThread.Priority = ThreadPriority.Lowest;
backgroundThread.Start();
Thread.Sleep(5000);
  1. Run your console application again. This time, you will see that the thread priority has been set to the lowest priority:
  1. Go back to your 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");
}
  1. When you run your console application, you will see that the thread is aborted before the Thread.Sleep method is called. Aborting a thread in this way, however is, generally, not recommended:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.151.220