c# (Csharp) Threading Interview questions :- What is AutoResetEvent and ManualResetEvent ?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will touch base on two important concepts of threading Auto reset event and manual reset event in some of the previous videos of threading you know we saw how we can do synchronization by using lock monitor mutex semaphore semaphore slim excetera etcetera now there is one more way by which you can do synchronization and that is by using the signalling methodology now both of these guys that is Auto reset event and manual reset event they help us to do synchronization by using the signalling methodology so let's first try to understand what exactly this signalling methodology means let's consider a simple example here let's say we have two threads here thread 1 and thread 2 and we want to implement synchronization you know between these two threads so what we can do here for synchronization is you know thread 2 can probably send a signal out to thread 1 saying that please go under a weight mode okay and then thread 2 can continue doing his work and when thread 2 finishes we can signal again to thread 1 saying that can you start from the place you know where you've halted right so in this way no by using the signaling methodology you know we can what you call implement synchronization between threads so both of these guys that is Auto reset event and manual is it even in who helps us to achieve that so let's first go ahead and see a simple example of auto reset event and then we'll see a simple example of manual reset event and then we'll try to understand the differences between them so let's go ahead first and create a very simple method you are called as some method okay we are trying to first demonstrate Auto reset event and then we will demonstrate manual reset event let me just make this font bigger right and over here what I'll do is I'll just say a console dot a write line I'll just say starting and finishing right simple so what we'll do is let's go ahead and invoke this sum method inside the thread so the first thing what you need to ensure is that you have the system dot threading namespace import it in your application which I have already done so I'm going to go here and say new thread and let's pass the method name here saying some method and I'll say dot start all right so this syntax you know what it will do is it will invoke some method in a different thread right right now when this application runs there are two threads which are created the first thread is nothing but the thread which actually runs your main application or I'll say which actually runs this static void main and the second thread is actually you know which you have recently created in other words which you have explicitly created over here in order to demonstrate Auto descent even in a proper manner we'll do the following the static void main we'll go and invoke this new thread and this new thread will actually go and run this some method and this some method currently what it is doing is it is actually just displaying you know starting and then finishing ok so what we'll do is as soon as this thread runs right over here on this line on on after you know the starting is displayed we'll go and make this thread wait now this waiting thread will be will be signal somewhere from here you know means after the thread has started somewhere in the static void mean will go and send a signal to start again ok so let's see that how we can use you know auto reset event you know to accomplish this so the first thing is we have to go and create a object of auto reset event over here ok so let's go ahead here and create objects so saying static auto reset event obj auto equal to new auto reset event false we'll we'll talk about this false later on right now in order that a threat goes in a wait mode you know what we can do is we can call the weight one method of the auto reset even object so what we can do is the place where we had planned to call the weight right we can say here obj Auto dot weight one so as soon as you know the thread comes and execute this line of code right what will happen is if we make the thread to go in halt mode or if we if we make that threat to go in a hanging mode okay now in order to go and signal you know to the thread and to say that you know restart again what we can do is in the static void main where we are planned to signal to start again will call here obj Auto dot set so as soon as this set is called over here he will send the signal from this main thread to the some method thread and say that please restart again or please start from the place you know where you are you have gone in a weight mode okay and what I'll do is you know I'll put a console dot read line over here so that you know we can you know go and invoke the set method after some time so that you know we can see that you know basically how the some method goes in halt mode and then we can see that after we press enter you know how it is revoked back so what will happen now the static void main will run it will go and invoke this new thread the new thread will start running it will display this console dot write line on the console on your monitor and then it will go in a halt mode after that you know just you know when I go and press into enter on the static void main thread it will actually call the set function which will go and you know revoke this thread again and he will then go and display finishing okay so let's go ahead and run this application so I'm going to go and do a control f5 here right so there it goes now you can see just for everyone's benefit so we are at this state now the static void main ran it went ahead and invoke this thread he displayed starting on the screen over here and he went under a halt mode or in a weight mode and now we are at this console dot read line go and press enter what will happen is it will go and send a signal to this weight one and it will then go and display finishing so if I press enter over here you can see he has now gone ahead and revoked that thread and it has displayed us finishing so in simple words Auto reset even class actually helps you to achieve synchronization by using the signalling methodology now there is one more class called as a manual reset event and it also does the same thing it also helps us to achieve synchronization by using signaling so the question would be now so what's the difference between Auto reset event and the manual reset event so in order to understand manual reset event in a proper manner let me go and you know tailor this code a bit okay and then we will go ahead with the manual reset event what I'll do is I'll go and call this wait 1 2 times so this is 1 wait 1 and I will call this wait 1 again so must be I can just number it saying okay this is starting 1 this is finishing 1 this is starting - and this is finishing - so now we have 2 places you know where the thread can go in a halt mode ok over here this is first and this one is second now I would like to make a statement here for every weight one called there should be a set to release it in other words for example at this point when you call weight 1 you can see that there is already a set to release it so this will actually go and release the weight 1 at 1 okay now when this second weight one is called right there is no set to release here so what will happen is you know this thread will go into hanging more forever right so what we'll do is we'll again go and paste this thing over here and we'll say that this will go and release weight one at two okay so for every weight one we should have a set which actually goes and releases it so you can see now if I go and run the application if I do a control f5 here all right so that's actually the first weight one so I do enter you can see now the first weight one is released now there is a second weight one if I do enter here now the second weight one is released so for every weight one you know we should have a set you know which will actually go and release that weight one okay nice now let's talk about manual reset event okay now again the syntax of manual reset event is absolutely same in other words I can just go and replace this whole thing like manual reset event new manual reset event right okay and you know even the methods are same now that you can see that we have a set here as well you know you can see we have a weight one here as well so absolutely you can see I should be able to compile this statement as it is a compile this change as it is so you can see that you know just I went ahead and I changed the auto reset event to manual reset event and there are no errors okay now let's see what happens okay now if I do a control f5 so that is my first weight one running now watch very closely or what happens because this is the main difference between auto reset event and manual reset event if I press I enter here I just pressed it once you know with one set it actually went ahead and revoked you know all the weight ones in simple words when you use auto reset event for every weight one you have to call a set but when you use a manual reset event you know when you say okay this is dot set right it actually just opens a gate and anybody who calls oh wait one right you know will not wait actually it will just go and run the thread so in simple words you know if you visualize right auto reset event is like a turnstile Gate a turnstile gate is a gate you know where only one person can enter at one moment of time while manual reset event is like an ordinary gate you know when it is open everybody can just rush in okay so in other words the difference between auto reset event and manual reset event is you know in Auto reset event you know need to call set on every weight one while in Mandal recent event you know one set actually goes and allows all the threads to run so i hope that you enjoyed this video in this video we were trying to understand you know what is the use of auto reset event manual reset event and how they differ from each other thank you so much now here's a small favor you can do for us or i'll say it's a small request from us okay if you think that you know whatever we are doing here on this channel is cool it is nice you know it will help out people what you can do is you can go ahead and share this video you know either on your Facebook account on your Twitter account on your or code on blogger you know whichever channel you are associated with please do go ahead and talk about this video by doing this small favor you know you are helping us to know that what activity we are doing is it worth for the community or not so go ahead you know if you like this video share it on a Facebook account Twitter blogger myspace or code Google+ whatever it is you know and let the world know that here's a resource of videos you know which dotnet developers can see and they can learn from it
Info
Channel: .NET Interview Preparation videos
Views: 86,946
Rating: undefined out of 5
Keywords: .NET, C#, threading, AutoResetEvent, ManualResetEvent, Interview Questions, csharp, csharp threading, csharp interview questions, csharp training, Learn .NET, Step By Step .NET, Learn C#, Step By Step C#
Id: xaaRBh07N34
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Tue Jun 19 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.