.Net Interview Questions and Answers

Search:

.NET interview questions: - How to parse data in Threading?

By : Shiv Prasad Koirala | Oct 31st, 2011 | Views : 522 | Comments : 0 Bookmark and Share

This is a semi-asked .NET interview questions and has been asked in most of the interviews to check your skills on parsing data in threading.

 

As a senior developer you would be interested to know how exactly we can parse data in threading.

 

Below is the code snippet for simple use of threading: -

 

    class Program
    {
        static void Main(string[] args)
        {
            Thread objThread = new Thread(CallMe);
            objThread.Start();
        }
        public static void CallMe()
        {
            for (int i = 0; i <= 5; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
        }
    }

In the above code snippet you can see that I have created a “CallMe” function which has a for loop and getting executed till the value of “i” is 5 and simply print a line of text to the output screen after every 2 seconds using Thread.Sleep(2000);.

 

Now, when you execute this above code snippet you will see result like below diagram.

 

data

 

The above code snippet seems to be works fine but what if there is a situation that you want to execute for loop according to the data passed by the Thread.

The data can be parsed in threading by the following two ways

 

1. You can parse the value by defining object variable.

 

data

2. You can use Lambda Expression.

 

So, let’s take the above point’s one by one try to understand it in better manner.

 

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

 

Step1: - Create a simple Console Application for that just open Visual Studio >> go to >> File >> New >> Project >> Windows >> Select Console Application.

 

data

data

 

Step2: - let us first prove the point of passing the data using object variables.

 

Now, simply just add the below code snippet in to program.cs file of your Console Application.

   
 class Program
    {
        static void Main(string[] args)
        {
            Thread objThread = new Thread(CallMe);
            //Passing the value 5.
            objThread.Start(5);
        }
        //Created a CallMe function with a Object parameter "k".
        public static void CallMe(object k)
        {
            //creted a int type variable and type caste it.
            int j = (int)k;
            for (int i = 0; i <= j; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
            Console.ReadLine();
        }
    }

 

Now, just simply execute your console application and will see result like below diagram.

 

data

 

This is how you can parse the data using Object Variables.

 

Step3: - similarly, let’s take the second point of parsing the data using Lambda Expression.

 

Now, simply just change your program.cs file code snippet like below code snippet.

 

    class Program
    {
        static void Main(string[] args)
        {
            //Created a Lambda Expression and parse the value as 7.
            Thread objThread = new Thread(() => CallMe(7));
            objThread.Start();
        }
        //Created a CallMe function with a int parameter "k".
        public static void CallMe(int k)
        {
            //Created a int type variable which hold the value of k.
            int j = k;
            for (int i = 0; i < j; i++)
            {
                Console.WriteLine("Threading.......");
                Thread.Sleep(2000);
            }
            Console.ReadLine();
        }
    }

 

In the below diagram you can see that how I have used the Lambda Expression in order to parse the data in Threading.

 

data

 

Now, when you run your Console Application you will see the result like below diagram.

 

data

 

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

 

 

Click for more .NET interview questions.

 

Regards,

 

Visit for more author’s blog on .NET interview questions

 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
0 1 1 2 1
 
 
 

ASP.NET interview questions: - Can you explain Method of Sorting GridViewcontrol in ASP.NET?

Sorting allow you to sort the GridViewcontrol data in Ascending or Descending order.... Read More

WCF Interview questions:- Which binding do we need to use for WCF REST?

In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More

.NET interview questions: - Can you elaborate project life cycle?

In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More

.NET interview questions: - How will you distinguish between ForeGround and BackGround Threading?

threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time.... Read More

C# interview questions: - Explain anonymous methods in .NET?

n simple words Anonymous Methods means method which are coded inline or methods without method name.... Read More

ASP.NET interview questions: - Can you explain Method of Sorting GridViewcontrol in ASP.NET?

Sorting allow you to sort the GridViewcontrol data in Ascending or Descending order.... Read More

WCF Interview questions:- Which binding do we need to use for WCF REST?

In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More

.NET interview questions: - Can you elaborate project life cycle?

In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More

.NET interview questions: - How will you distinguish between ForeGround and BackGround Threading?

threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time.... Read More

C# interview questions: - Explain anonymous methods in .NET?

n simple words Anonymous Methods means method which are coded inline or methods without method name.... Read More

Article Categories