.NET interview questions: -Elaborate differentiation 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 important Dotnet interview questions for preparation

Regards,

View author’s other Most asked Dotnet interview questions

 

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s