471,305 Members | 1,217 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 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 1916
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Clark Sann | last post: by
2 posts views Thread by www.brook | last post: by

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.