473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Interesting 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 16 '05 #1
1 2181
Hi Jeff,
It is documented at:

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemthreadingmonitorclassentertopic.asp

--
Keiji Oenoki
Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.
--------------------
From: "Jeff Gerber" <jg******@tampabay.rr.com>
Newsgroups: microsoft.public.dotnet.languages.vc
Subject: Interesting 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 16 '05 #2

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

Similar topics

4
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...
1
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...
6
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
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
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
by: Shawn B. | last post by:
Greetings, What is the difference between lock(...) and Monitor.Enter(...) / Monitor.Exit(...) ? Thanks, Shawn
3
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.