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

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


In this video we will touch base on two important concept of Threading AutoResetEvent and ManualResetEvent In some of the previous videos of Threading you know we saw how we can do syncronization by using Lock, Moniter, Mutex, some of four some of slim etc etc Now that is one more way by which we can do syncronization and that by using the signaling mathedology Now both of these gies that is auto reset event and manual reset event the help us to do syncronization by using the signaling methodology so let’s first try to understand what exactly the signaling methodolgy means let’s consider a simple example here let’s say we have two threads thread one and thread two and we want to implement syncronization between these two threads so what we can do here first syncronization is thread two can probably send a signal out to thread one saying that please go under a wait mode and than thread two can continue doing is work and when thread to finishes you can signal again to thread one saying that can you start from the place where you have holded so in this way by using the signalling methodology we can implement synchronization between thread so both if this guyz that is auto reset event and manually reset event help us to achieve that so let’s first go ahead and see a simple example of auto reset event and then will see a simple example of manual reset event and then will try to understand the differences between them so let’s go ahead first and create a very simple method here called as some method we are trying to first demonstrate auto reset event and then we will demonstrate manual reset event let me just make this fonts bigger and over here what i will do is i will just say console.writeline i will just say starting and finishing so what will do is let’s go ahead and invoke this some method inside of thread so the first thing what we need to ensure is that you have the system.threading names space imported in your application which i have already done so i am going to go here and say new thread and let’s pass the method name here saying some method and i will say dot start so this syntax what it will do is it will invoke some method in a different thread 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 will say which actually runs this static void main and second thread is actually which you have recently created in other words which you have specifically created over here in order to demonstrate auto reset event in a proper manner will do the following the static void main will go and invoke this new thread and this new thread will actually go and run the some method and this some method currently what it is doing is it is actually just displaying starting and then Finishing OK so what will do is as soon as this thread runs over here on this line after starting displayed will go and make this thread wait now this waiting thread will be singled somewhere from here means after the thread started somewhere in the static void main will go and send a signal to start again OK so let’s see that how we can use auto reset event to accomplish this so the first thing is we have to go and create a object of auto reset event over here so let’s go ahead here and create object saying static auto reset event obj auto equal to new auto reset event false will talk about this false later on now in order to thread goes in a wait mode what we can do is we can called the way to one method of the auto reset event object so what we can do is the place where we are planed to call the wait we can say here obj auto dot wait one so as soon as threads comes and execute this line of code what will happen is it will make the thread to go in a hold mode or it will be make that thread to go in hanging mode now in order to go and signal to the thread and to say that restart again what we can do is in the static void main where we have planed to signal to start again will call here objAuto.set so as soon as this set is called over here he will send the signal from this main thread To the some method say that please restart again or please start from place you know where you have gone in a wait mode. And what I will do is you know I will put a console.readLine over her so that you know we can go and invoke the set method after some time So that you know we can see that basically how the some method goes in halt mode and then we can see that after we press enter you know how it is revoke back So what will happen now the static main will run it will go and invoke this new thread The new thread will start running. it will display this Console.WriteLine on the console on your monitor and then it will go in to halt mode After that you know when i go and press enter on the static void main thread it actually call the set function We will go and you know revoke this thread again and he will then go and display fininshing So lets go ahead and run this application so I am go to do ctrl F5 here Now you can see just for every ones benefit So we are at the state now The static void Main ran it went ahead and invoke this thread he display starting on the screen over here and he went under a Holt mode or in a wait mode and now we are at this console dot read line now if i go and press enter what will happen is it will go and send a signal to this wait 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 revoke that thread and it has display us finishing so in simple words auto reset event class actually helps us to achieve synchronization by using the signaling methodology now there is one more class called as a manual reset event and it also does the same thing it also help us to achieve synchronizing by using signaling so the questions would be now so what’s different 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 trailer this code a bit ok then we will go ahead with the manual reset event what i will do is i will go and call this wait one two times so this is one wait one and i will call this wait one again so must be i can just be numbered saying ok this is starting one this is finishing one this is starting two and this is finishing two so now we have two places es where the thread can go in a Holt mode over here this is first and this one is second now i would like to make statement here for every wait one called there should be a said to release it in other words for example at this point when you called wait one you can see that there is a already a set to release it so this will actually go and release the wait one at one ok now when this second wait one is called there is no set to release here so what will happen is this thread will go in a hanging mode for ever so what will do is we will again go and paste this thing over here and will say that this will go and release wait one at two so for every wait one we should have a set which actually goes and release it so you can see now if i go run the application if i do Ctrl+F5 here so that’s actually the first wait one so i do enter you can see now the first wait one is released now there is a second wait one if i do enter here now the second wait one is released so for every wait one we should have a set which will actually go and release that wait one now let’s talk about manual reset event 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 and even the method are same now you can see that we have set here as well you can see i have wait 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 you can see that just i want ahead and and i changed the auto reset event to manual reset event and there are no errors now let’s see what’s happens now if i do a Ctrl+F5 so there is my first wait on running now watch very closely what happens because this is a main difference between auto reset event and manual reset event if i press a enter here i just pressed it once with one set it actually went ahead and revoked all the wait once In simple words when you use auto reset event for every wait one you have to call aset But when we use the manual reset event when you say OK this is dot set it actually open the gate and any body who calls a waitone you know will not wait actually will just go and run the thread So In simple words if you visualise auto reset event is like a turnstile gate A turnstile gate is the gate where only one person can entered one moment of time while manual reset event like a ordinary gate where it is open every body get Russ in So in other words the difference between auto reset event and manual reset event is you know in auto reset event we need to call set on every wait one while in manual reset event one set actually goes and allows all the threads to run So I hope enjoy this video in this video we will trying to understand what is the use of Auto reset event, manual reset event and how the differ from each other Thank you so much.

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

Leave a Reply

Leave a Reply

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