c# (Csharp) threading interview question:- What is thread,background thread and foreground thread ?


In this video we will try to understand what exactly is Threading in C# and also will see a simple example of the same and then at the later end of the video we will try to understand 2 important types of Threads one which is called as the foreground thread and the other one which is called as the background thread so lets first start with what is threading and then we will move towards the different types of thread that is foreground and background. threading in C# means parallel code execution In other words lets see in C# programme you have multiple function like function1 function2 or you have lot of logic and sometimes there are situation where you would like to run this logic parallel so that what is called as threading so in order to understand how C# actually runs these codes parallely lets create a simple console application in C# and let see a simple example and lets demonstrate threading so here the simple console application to start of with this console application has 2 functions one is function1 and function2 and both of these functions have a loop which runs 10 times and displayes the name of the functions and the current running number so for example if you are inside function 2 it will display function 2 executed and the running number what it is the eye count if you are inside function 1 it will display function 1 executed and the eye count what is running currently and both of these function are called from static void main that is from the main programme so if i do a Ctrl5 here you can clearly see that its first executing function one and then it is executing function 2 . Now you can clearly see from the programme that first function 1 runs and then function 2 runs so in other words it is a synchronous processing Synchronous means sequential processing in other words until function 1 does not finish function 2 does not start Now there can be satiation where would like to run both of these functions parallely in other words you would like to have some kind of mechanism where you would like to say OK function 1 and function 2 both of you guise will run parellely in other words you would like your computer processor to give some time to function 1 and then some time to function 2 so rather than executing sequentially you would like to see here function 1 executed one and then function 2 executed and then again function 1 executed and that’s where we use threading So lets go step by step and let see how we can implement threading in this project the fist thing is you need to go and import the name space of threading so say using System.Threading this is the name space where you have a thread object which helps you to execute function or your logic parellely also rather that invoking these function sequentially we would like to go and invoke them in a multi threaded manner or in a parallel way right in order to invoke function 1 and function 2 in a parallel way wee need to use something called as the thread object so let me go and create a thread object now in this thread object you need to pass the parameter which is nothing but the function name so i am go to say OK this thread on this thread will actually go and run function 1 and will get one more thread object here called as obj2 which will actually run function 2 right so we have created two thread objects So that both of these function can run parallely one is obj1 and other one is obj2 obj1 points towards function1 and obj2 point towards function2 and the next thing is we need to go and start both of these thread so we can say obj1.start so this actually goes and invokes a separate thread on which function1 will actually start executing and we would also like to go and start a second thread on which we go to run function 2 so we first went ahead and created two threads one for function 1 and the other one for function 2 and then is the next step be invoked these threads in order to see a clear execution what i will do is after every for loop which runs right every for variable executed inside function1 as well as function2 i will wait for must be for 4 seconds now in order to wait for 4 seconds i am go to again use the thread class here and i am go to say here thread.sleep and you can see that we need to pass here milliseconds so i am go to say 4000 so 1 second=1000 millisecond so we can go to here for 4 second and also i would like to wait even in function2 for every for loop execution again for 4 seconds so that you can see this output clearly how there executing in multi threaded manner or in a parallel manner so we have written the complete application now you have created two thread objects attached with function and function2 and second as the for loop is running we went to for 4 second to see how the output actually looks like and now let me just do a CtrlF5 here and now you can clearly see that function2 has executed first Ok and then function1 then again for the next for loop function2 has executed and then function1 so you can see clearly now if you remember right the previous output where function1 was executing first and then function2 was executing so it was completely sequential here you can clearly see that the computer processor is giving some time to function1 and then it is giving sometime to function2 In other words it is running both of these functions parallel or i will say rather in the multi threaded manner so that what is the use of threading by using threading you can execute programme codes in a multi threaded way or in a parallel way now as we said in the previous start of the video there are two kinds of thread one is the foreground thread and the other on is the background thread till now what ever thread object we have created are type of foreground thread so by default when ever you create thread like this its a fore ground thread so both of these thread that is obj1 and obj2 are fore ground threads Now what exactly the fore ground thread now fore ground threads are thread which will keep running even though if your main application exits or main application quits in other words our main application starts from static void main so that’s are main thread OK now even if your main application quits over here like this is the point when main application will quit OK then also your foreground threads will execute until the finished that there task now lets talk about back ground thread, Back ground thread are threads which will quit if your main application quit so in other words until your main application is alive the background thread is alive the time your main application quits even the back ground thread quits Now what will do is lets go and do a simple sample demonstration understand both of these concepts in a practical manner so what will do is lets make this code more simpler so i am go to and delete this function2 over here i am go to have only this function1 and only thread for that so all of the threads i am go to just delete it so very simple application here we have only one thread and this thread actually goes and response function1 and in this what i will do is i am to put the Console.Write here saying Function1 is executing OK and here what i will do is i am go to do a Console.ReadLine here so that this says that yes the Console1 is still executing and here i will put a Console.WriteLine here stating that the main application has exited OK also let me just say here that run function1 is exited so much i will say that this is entered and this is exited function1 right so how will this programme execute now OK first thing what will happens a thread will be created to run function1 right to run function1 parallely right after that ones is function1 is run the control will come here state forward in other words it will exit the Console main application it will exit the main function the static void main function now when the threads starts executing function1 right now what will happen here over here the control will come to this line that is Console.ReadLine and we will wait here until the user puts any input right Now if you run a fore ground thread if you run this thread in a fore ground manner right what will happens even if the application quits over here still this Console.ReadLine will be displayed you will able to enter the number and until the funtion1 is exited this function will keep running OK if you run this as a background thread what will happens it will start function1 but then it will completely go off OK So currently by default this thread is a fore ground thread OK by default when ever you create a thread its a fore ground thread so let me do a CtrlF5 here and let see what happens so you can see now the main application has already exited in other words this command has already will fired Ok and it is now waiting it is now waiting on function it has now executed function1 is entered and it is waiting here add Console.ReadLine to enter something OK if i enter here something then you can see now function1 as exited in other words the thread which was running function1 continuous running even if your main application as exited this is called as the foreground thread now lets go ahead and create a background thread so in order to make this thread as background what you do say is obj.IsBackground=true, now see what happens now if i do a CtrlF5 here you can see function1 has entered it has entered function1 but after that its just saying exit the whole application Ok so in other words it is not going to display me this one that is function1 has exited OK in other words it has stopped executing the thread why because the main application is getting exited so if i do a key press here you can see that the complete application has exited in other words the back ground thread when you create a thread in a background manner all those thread will get they will die off if your main application dies off while the fore ground thread they will keep on running until the finished are logic so even if a main application exits the fore ground thread still keeps on running until its finishes its logic

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *