I have a method StartProcess()
. I want this method to throw an exception if the same method is called by another thread at the same time or by the same initial thread before EndProcess()
is called.
I tried the Monitor
class but I wasn't sure how to apply it to the above stated case. What I saw that was close to what I was looking for was:
var obj = new Object();
// Define the critical section.
Monitor.Enter(obj);
try {
// Code to execute one thread at a time.
}
// catch blocks go here.
finally {
Monitor.Exit(obj);
}
I need guidance to handle my exact stated scenario. I've been trying all day but couldn't get it to work.