I would like to
Monitor.Enter(this.StaticLock);
try
{
// Do something ...
ThreadPool.QueueUserWorkItem(state =>
{
try
{
// Do something else...
}
finally
{
Monitor.Exit(this.StaticLock);
}
});
}
catch (Exception)
{
Monitor.Exit(this.StaticLock);
throw;
}
But it doesn’t work since the it can’t Monitor.Exit on an object that wasn’t Monitor.Enter in the current thread. How to do that? Should I use interthread communication?
Semaphores allow you to lock them in one thread and unlock them in another.But this kind of behavior is very suspicious to me… What exactly are you trying to accomplish? This should almost never be done in practice.