PC Review


Reply
Thread Tools Rate Thread

why use Monitor.Enter...Monitor.Exit insted of lock(object9

 
 
Tony Johansson
Guest
Posts: n/a
 
      15th May 2010
Hi!

If I want to lock a section in the code from concurrency item problems I can
use the static Monitor class in this way
Monitor:Enter
....
Monitor.Exit

I can also use the C# lock keyword to lock the same section.

So I don't see any point at all to use the Monitor.Enter... Monitor.Exit
instead of the lock keword just to lock a section in the code because of two
reasons.
1. It much less to write lock then the other
2. The most importand it that you get the try..finally for free which mean
that you will always
free the lock in case of running into same kind of exception

Is this correct understood ?

//Tony


 
Reply With Quote
 
 
 
 
Matt
Guest
Posts: n/a
 
      15th May 2010
On May 15, 6:01*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> Hi!
>
> If I want to lock a section in the code from concurrency item problems I can
> use the static Monitor class in this way
> Monitor:Enter
> ...
> Monitor.Exit
>
> I can also use the C# lock keyword to lock the same section.
>
> So I don't see any point at all to use the Monitor.Enter... Monitor.Exit
> instead of the lock keword just to lock a section in the code because of two
> reasons.
> 1. It much less to write lock then the other
> 2. The most importand it that you get the try..finally for free which mean
> that you will always
> free the lock in case of running into same kind of exception
>
> Is this correct understood ?


Yes. It makes more sense to write:

lock(obj)
{
// Code
}

because this gets expanded into:

Monitor.Enter(obj)
try
{
// code
}
finally()
{
Monitor.Exit(obj)
}

(Forgive typos, I'm just typing it in).
So, lock is exception friendly, whereas just using Monitor Enter/Exit
requires that you
write your own exceptioni handling.

Matt
 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      15th May 2010
Hello,

At http://msdn.microsoft.com/en-us/library/aa664735(v=VS.71).aspx, you'll
find :

"A lock statement of the form

Copylock (x) ...
where x is an expression of a reference-type, is precisely equivalent to

CopySystem.Threading.Monitor.Enter(x);
try {
...
}
finally {
System.Threading.Monitor.Exit(x);
}"

--
Patrice

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      15th May 2010
On 15-05-2010 08:01, Tony Johansson wrote:
> If I want to lock a section in the code from concurrency item problems I can
> use the static Monitor class in this way
> Monitor:Enter
> ...
> Monitor.Exit
>
> I can also use the C# lock keyword to lock the same section.
>
> So I don't see any point at all to use the Monitor.Enter... Monitor.Exit
> instead of the lock keword just to lock a section in the code because of two
> reasons.
> 1. It much less to write lock then the other
> 2. The most importand it that you get the try..finally for free which mean
> that you will always
> free the lock in case of running into same kind of exception
>
> Is this correct understood ?


Using the two Monitor methods gives you more
control than the lock { }.

In most cases lock { } is fine, but in some cases
you may want more control - like doing Monitor.Exit
different places depending on some variables.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger Microsoft ASP .NET 2 2nd Dec 2006 04:46 PM
problem to use DDR insted of SD RAM Rushi DIY PC 3 13th Dec 2005 12:36 AM
problem to use DDR insted of SD RAM Rushi Computer Hardware 3 13th Dec 2005 12:36 AM
Win XP Restarting Insted of Shutdown Carlos Windows XP General 3 8th Sep 2003 09:35 PM
How do I use address to get a area insted of a single address Klaus Rasmussen Microsoft Excel Worksheet Functions 0 2nd Sep 2003 11:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 PM.