Tuesday, September 27, 2011

.NET interview questions: - What are the difference between ForeGround and BackGround Threading?

Threading: - threading is a parallel processing unit and helps you to
access multiple tasks at a one moment of time.

A managed thread is either the Foreground thread or a Background thread.
Background threads are identical to foreground threads with one exception: a
background thread will not keep the managed execution environment alive. Once
all foreground threads have been stopped in a managed process, the system stops
all background threads and shuts down.

The following points will show the difference between the Foreground and
Background thread.

1. Foreground threads have the ability to prevent the current application
from terminating. The CLR will not shut down an application until all
Foreground
threads have ended.

2. Background threads are viewed by the CLR as expendable paths of
execution that can be ignored at any point in time. Thus, if all Foreground
threads have terminated then all Background threads are automatically
killed when the application domain unloads.

Now, let’s create a simple demonstration to see the exact difference between the
Foreground and Background thread.

In order to see it practically just follow the following steps.

Step1: - Create a new project of Console Application for that just go to
>> File >> New >> Project >> Windows >> Select
Console Application.







Step2: - Import the namespace using System.Threading; in your
program.cs file.



Step3: - Now just add the below code snippet in your program.cs file.

using System.Threading;

namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Test objLongTest = new Test(20);// Passing the value as 20.
//just called the Function1 in the foreground thread.
Thread fgThread = new Thread(new ThreadStart(objLongTest.Function1));
fgThread.Name = "Foreground Thread";//assigned the Name to the thread.
Test objShortTest = new Test(10);// Passing the value as 10.
//just called the Function1 in the background thread.
Thread bgThread = new Thread(new ThreadStart(objShortTest.Function1));
bgThread.Name = "Background Thread";//assigned the Name to the thread.
bgThread.IsBackground = true;//set the IsBackground property as true.
fgThread.Start();//Started the Foreground thread.
bgThread.Start();//Started the background thread.
}
class Test
{
int Iteration;
public Test(int Iteration)
{
this.Iteration = Iteration;
}
public void Function1()//created a function.
{
String threadName = Thread.CurrentThread.Name;
for (int i = 0; i < Iteration; i++)
{
Console.WriteLine("{0} count: {1}",
threadName, i.ToString());
Thread.Sleep(350);
}
//the below line will print the thread name to the output screen.
Console.WriteLine("{0} finished counting.", threadName);
Console.ReadLine();
}
}

}
}

Step4: - Now, just run your Console Application and will see result like below diagram.





The above diagram proves that the Foreground thread has the ability to
prevent the current application from terminating.

Watch the following video on thread, background thread and foreground thread in
.NET: -




See our other interview questions and answers for .NET for preparation


Regards,

View author’s other Most asked Dotnet interview question

No comments: