473,507 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected lock/Monitor.Enter() behavior



/*

This test program will give a "Value cannot be null" error.

If the lock in this code is removed (or Monitor.Enter()) the program will
run as expected.

I have found no explaination in lock() or Monitor.Enter() documentation as
to why this occurs. I suspect that it is by design of the behind-the-scenes
process that lock uses and is not a bug, however, it would be nice if there
was some documentation to illuminate this process.

If anyone knows otherwise the knowledge is greatly appreciated.

*/

using System;

using System.Threading;

namespace testdelegate

{

delegate void testDelegate();

class Class1

{

private static testDelegate myPrivateDelegate;

public static testDelegate myPublicDelegate

{

set

{

//Monitor.Enter(myPrivateDelegate);

lock(myPrivateDelegate)

{

myPrivateDelegate = value;

}

//Monitor.Exit(myPrivateDelegate);

}

}

[STAThread]

static void Main(string[] args)

{

myPublicDelegate = new testDelegate(DelegateMethod);

lock (myPrivateDelegate)

{

if (myPrivateDelegate != null)

myPrivateDelegate();

}

}

public static void DelegateMethod()

{

Console.WriteLine("executed");

}

}

}

Nov 15 '05 #1
1 5636
Jeff Gerber <jg******@tampabay.rr.com> wrote:
This test program will give a "Value cannot be null" error.


Yup - because the first time you try to enter the lock,
myPrivateDelegate is null, and you can't lock on null. Instead, create
another field:

object padlock = new object();

and always lock on that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
20720
by: Vinay C | last post by:
Hi, Can anyone clear me that, when should we use go for mutex, and in which situation should we opt for monitor, lock, semaphone and other objects, in a multithreaded application for synchronization...
6
437
by: n_o_s_p_a__m | last post by:
I have seen some debate but am not clear on what the problematic implications of the expression lock(this) are. Microsoft seems to advocate it, but others seem to be against it. Pros and cons? ...
14
6298
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
2
2682
by: Fernando Rodríguez | last post by:
Hi, I've been reading the multithreading tutorial at http://www.yoda.arachsys.com/csharp/threads/locking.shtml (thanks Jon :-) and there's one thing I don't understand. Why do have to pass a...
2
13178
by: Shawn B. | last post by:
Greetings, What is the difference between lock(...) and Monitor.Enter(...) / Monitor.Exit(...) ? Thanks, Shawn
1
2191
by: Jeff Gerber | last post by:
/* This test program will give a "Value cannot be null" error. If the lock in this code is removed (or Monitor.Enter()) the program will run as expected. I have found no explaination in...
3
5744
by: marvind | last post by:
Hello, Why doesn't the lock in Class2::Execute protect the critical section: public Class1 { ... public DataTable SchemaTable = new DataTable(); }
4
12332
by: Scott Johnson | last post by:
Hi I am converting some code from C# to VB.NET and I have come across a command that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from...
94
30186
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.