473,378 Members | 1,321 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,378 software developers and data experts.

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(object oState)
{
Add(input);
.... // code after Add(input)
Do(input); //if some condition met then Do()
...
}

....
public void Add(string input)
{
try
{
Monitor.Enter(strAdded);
this.strAdded += input;
}
catch (Exception oException)
{
throw oException;
}
finally
{
Monitor.Exit(strAdded);
}
}
....
Feb 8 '06 #1
3 1994
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**@discussions.microsoft.com> 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.com>
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
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...
6
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. ...
2
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...
2
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...
13
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.