469,963 Members | 2,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

how to check if Monitor::Exit has been called

Ian
Monitor::Enter and Monitor::Exit must be called in pairs. Is there a way
to determine if Monitor::Exit has been called so that it does not get called
a second time? The psuedocode below illustrates a situation I would like
to implement. Essentially there are 2 monitors and the first monitor can be
released in 2 different locations. This sample codes uses a booleen
variable to make certain the first monitor is not released twice. My
question is, does .NET offer a better way to check if a monitor object has
already been released/exited?

Thanks,

Ian
Monitor::Enter( resource1.Object );
Monitor::Enter( resource2.Object );
bool bMonitor1IsActive = true;

try {
// ...uses resources locked by resource1.Object and resource2.Object ,
may throw exceptions...

Monitor::Exit( resource1.Object );
bMonitor1IsActive = false;

// ...excute code locked to resource2.Object , may throw exceptions...
}
catch( Exception ^pE ) {
// ... handle exception ...
}
finally {
if( bMonitor1IsActive )
Monitor::Exit( resource1.Object );
Monitor::Exit( resource2.Object );
}

Nov 10 '06 #1
2 1588
"Ian" <Ia*******@yahXX.comwrote in message
news:qk********************@weber.videotron.net...
Monitor::Enter and Monitor::Exit must be called in pairs. Is there a way
to determine if Monitor::Exit has been called so that it does not get
called a second time? The psuedocode below illustrates a situation I
would like to implement. Essentially there are 2 monitors and the first
monitor can be released in 2 different locations. This sample codes uses
a booleen variable to make certain the first monitor is not released
twice. My question is, does .NET offer a better way to check if a monitor
object has already been released/exited?
That's exactly what the lock keyword is for

lock (resource2)
{
lock(resource1)
{
// code that needs both
}

// code that only needs resource 2
}

-cd
Nov 10 '06 #2
Ian
Hello Carl,

Are you referring to the 'lock' class in the C++ support library? I just
found it in the online help and will take a look at it.

Thanks,

Ian
Nov 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by bughunter | last post: by
8 posts views Thread by Michael Kennedy | last post: by
6 posts views Thread by Clark Sann | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.