473,748 Members | 10,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Monitor.Enter/Exit Question

Hi

I've got a CollectData() method which is called by Timer periodically.
CollectData calls Add(input) - the problem is the code after Add(input) in
CollectData() is never executed. It's as if the monitor is never closed.
CollectData() never executes further then Add(input), except I remove the
monitors. It's an Windows CE 5.0 application.

What is my problem? Hope someone can help.

Thanks!

....
protected void CollectData(obj ect oState)
{
Add(input);
.... // code after Add(input)
Do(input); //if some condition met then Do()
...
}

....
public void Add(string input)
{
try
{
Monitor.Enter(s trAdded);
this.strAdded += input;
}
catch (Exception oException)
{
throw oException;
}
finally
{
Monitor.Exit(st rAdded);
}
}
....
Feb 8 '06 #1
3 2009
Rudi wrote:
Hi

I've got a CollectData() method which is called by Timer periodically.
CollectData calls Add(input) - the problem is the code after Add(input) in
CollectData() is never executed. It's as if the monitor is never closed.
CollectData() never executes further then Add(input), except I remove the
monitors. It's an Windows CE 5.0 application.

What is my problem? Hope someone can help.


What kind of timer are you calling the CollectData method from (there
are three timers in the framework)? Does this timer fire from another
thread?

I think the problem is that the strAdded object gets changed by the +=
operator (as strings can't be modified, += returns a completely new
string / new object instead).

So in effect you are calling Monitor.Enter and Monitor.Exit on two
different objects and the Monitor will never be released on the initial
object.. which can lead to the dead lock you are experiencing.

Try to lock on some object that does not change and see if that helps!

hth,
Max
Feb 8 '06 #2
Thanks!!

That helped. It was the object I was locking.
"Markus Stoeger" wrote:
Rudi wrote:
Hi

I've got a CollectData() method which is called by Timer periodically.
CollectData calls Add(input) - the problem is the code after Add(input) in
CollectData() is never executed. It's as if the monitor is never closed.
CollectData() never executes further then Add(input), except I remove the
monitors. It's an Windows CE 5.0 application.

What is my problem? Hope someone can help.


What kind of timer are you calling the CollectData method from (there
are three timers in the framework)? Does this timer fire from another
thread?

I think the problem is that the strAdded object gets changed by the +=
operator (as strings can't be modified, += returns a completely new
string / new object instead).

So in effect you are calling Monitor.Enter and Monitor.Exit on two
different objects and the Monitor will never be released on the initial
object.. which can lead to the dead lock you are experiencing.

Try to lock on some object that does not change and see if that helps!

hth,
Max

Feb 9 '06 #3
Rudi <Ru**@discussio ns.microsoft.co m> wrote:
That helped. It was the object I was locking.


Note that if you use lock(...) instead of calling Monitor.Enter/Exit,
the compiler makes sure you lock/unlock the same reference anyway.

In your case, the code would be:

public void Add(string input)
{
lock (strAdded)
{
strAdded += input;
}
}

Locking on that monitor is a bad idea anyway though, precisely because
you're changing the variable value. See
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 9 '06 #4

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

Similar topics

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;
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
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
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.