473,748 Members | 8,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Monitor.Enter

4 New Member
Hi,

I just tried to convert my current project to the new beta 2 of the .net
framework and I encountered some problems with my thread synchronization -
that formerly worked well.

System.Threadin g.Synchronizati onLockException : Object synchronization method was called from an unsynchronized block of code.
Code is below

Monitor.Enter (collectionvar );

if ( collectionvar .Count == 0 )
collectionvar = value();

Monitor.Exit (collectionvar );//i am getting error here.

return collectionvar ;
Apr 25 '07 #1
4 3486
MyProblem
4 New Member
Hi guys,

Waiting for your soluitons.

Thanks in advance
May 10 '07 #2
Motoma
3,237 Recognized Expert Specialist
I have not done anything with Synchronization , but what may be happening is that your call to Monitor.Enter may be failing. Take a look at the MSDN and see what the possible results for that call are, and perhaps you can catch an error before an exception is thrown.


Hi,

I just tried to convert my current project to the new beta 2 of the .net
framework and I encountered some problems with my thread synchronization -
that formerly worked well.

System.Threadin g.Synchronizati onLockException : Object synchronization method was called from an unsynchronized block of code.
Code is below

Monitor.Enter (collectionvar );

if ( collectionvar .Count == 0 )
collectionvar = value();

Monitor.Exit (collectionvar );//i am getting error here.

return collectionvar ;
May 10 '07 #3
MyProblem
4 New Member
I have not done anything with Synchronization , but what may be happening is that your call to Monitor.Enter may be failing. Take a look at the MSDN and see what the possible results for that call are, and perhaps you can catch an error before an exception is thrown.
Thanks Motoma,
Atleast you initiate, I'm just wondering why this code is not working in VS2005 , earliere it was working in VS2003.

I have seen lot discussion on Monitor.enter/exit ...but not one give the what need to do here.
May 11 '07 #4
MyProblem
4 New Member
1 first run of code:I got some point here.....what happing here when nothinin in _cache (cach.count=0 )at the time of Monitor.enter.. ...and after loading value it has some value at Monitor.exit so raising error because object value is not same.

2:second run no error because object have same value.

why this happing in only ASP.NET 2.0



public static GetCountyCollec tion Cache
{
get
{
Monitor.Enter ( _cache );



if (_cache.Count == 0)
_cache =FilCounty();

Monitor.Exit ( _cache );



return _cache;
}
}
May 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5657
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 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
5
1690
by: ste | last post by:
this code not work: it is no possible Exit from Monitor Why ? thank ste //////// using System; using System.Collections;
12
1884
by: Brett Robichaud | last post by:
I need to make access to a reference object threadsafe. My natural instinct was to simply use Monitor.Enter() and Exit(). The problem is that the object behind the reference changes frequently, so my understanding of Monitor indicates this would not protect my object. Example: Bitmap bmp = new Bitmap("x.jpg"); Monitor.Enter(bmp); bmp = new Bitmap("y.jpg"); Monitor.Exit(bmp);
1
2203
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 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
5
2080
by: Ken Varn | last post by:
If I have a value type such as int that I want to protect in a multi-threaded situation, Is it safe to use Monitor::Enter(__box(value))? I am thinking that a different object pointer is generated each time and thus the protection is not insured. Is this true or not? Example: public _gc class MyClass { int Value;
8
1719
by: Michael Kennedy | last post by:
Hi, I have been looking into the claim that the keyword lock is not safe when exceptions are possible. That lead me to try the following code, which I think has uncovered a serious error in the .NET 2.0 framework. Note that this runs better, but not perfectly, on .NET 1.1. Note: The numbers are line numbers needed to match the callstack of the exception below.
6
2145
by: Clark Sann | last post by:
Can someone help me understand what object should be used as the lock object? I've seen some programs that use Monitor.Enter(Me). Then, in those same programs, they sometimes use another object. Why? I would appreciate it if someone could help me understand the ramifications of picking an object in this command. Thank you so much for your help. Clark
2
1721
by: www.brook | last post by:
I have a VC++.net code. Two threads are created, one thread keeps appending elements to the queue, another keeps deleting an element from the queue. I tried to use monitor, but it seems that the there is problem. in the thread of the deleting, seems that the deleting function entered twice and both try to delete the queue. The first thread find it is not empty, and try to delete an element, but before this thread actually delete it,...
2
1783
by: Ian | last post by:
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...
13
2678
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a solution. But this question sticks to my head as a conceptual problem) Suppose there are two threads T1, T2 running concurrently:
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
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 we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.