Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Foreground Threads vs. Background Threads

The CLR considers every thread to be either a foreground thread or a background thread. When all the foreground threads in a process stop running, the CLR forcibly ends any background threads that are still running. These background threads are ended immediately; no exception is thrown.

Therefore, you should use foreground threads to execute tasks that you really want to complete, like flushing data from a memory buffer out to disk. And you should use background threads for tasks that are not mission-critical, like recalculating spreadsheet cells or indexing records, because this work can continue again when the application restarts, and there is no need to force the application to stay active if the user wants to terminate it.

The CLR needed to provide this concept of foreground and background threads to better sup- port AppDomains. You see, each AppDomain could be running a separate application and each of these applications would have its own foreground thread. If one application exits, causing its fore- ground thread to terminate, then the CLR still needs to stay up and running so that other applications continue to run. After all the applications exit and all their foreground threads terminate, the whole process can be destroyed.


The following code demonstrates the difference between foreground and background threads.

 

using System;

using System.Threading;

 

public static class Program { public static void Main() {

// Create a new thread (defaults to foreground) Thread t = new Thread(Worker);

 

// Make the thread a background thread t.IsBackground = true;

 

t.Start(); // Start the thread

// If t is a foreground thread, the application won't die for about 10 seconds

// If t is a background thread, the application dies immediately Console.WriteLine("Returning from Main");

}

 

private static void Worker() {

Thread.Sleep(10000); // Simulate doing 10 seconds of work

 

// The following line only gets displayed if this code is executed by a foreground thread Console.WriteLine("Returning from Worker");

}

}

 

It is possible to change a thread from foreground to background and vice versa at any time dur- ing its lifetime. An application’s primary thread and any threads explicitly created by constructing a Thread object default to being foreground threads. On the other hand, thread pool threads default to being background threads. Also, any threads created by native code that enter the managed ex- ecution environment are marked as background threads.

       
   
 
 

 

 


Date: 2016-03-03; view: 939


<== previous page | next page ==>
Nbsp;   Thread Scheduling and Priorities | Nbsp;   Introducing the CLR’s Thread Pool
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)