469,573 Members | 1,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

Monitor, Lock

I have a main form, which pop other forms (children forms).
The children forms have a delegate of a function, let call it EndFunction,
of the main form, that they call when they ends their work.
By closing the main form, it force the children forms to close too, and
before to close himself wait until the last children form has closed.
When the children forms are force to close, they will call so rush the
EndFunction that I have a synchronization problem with it.
I tried to use the Monitor.Enter / Exit and the lock(..){} in the
EndFunction, but I had a lock in the application.
By implementing a very simple lock mechanism it work very well:

private bool busy=false;

public void EndFunction()
{
while(busy) System.Threading:Thread.Sleep(100);
busy=true;
...
busy=false;
}

Is that the only way to synchronize a bit of code without lock the entire
object?
Nov 15 '05 #1
5 1739
Zürcher See <aq****@cannabismail.com> wrote:

<snip>
By implementing a very simple lock mechanism it work very well:

private bool busy=false;

public void EndFunction()
{
while(busy) System.Threading:Thread.Sleep(100);
busy=true;
...
busy=false;
}

Is that the only way to synchronize a bit of code without lock the entire
object?


No - just have lots of different locks, one for each thing you're
interested in. You don't have to use things like lock (this) - and I
generally argue that you shouldn't. I tend to make the lock private,
and a reference which nothing else will have.

Note that your code above isn't thread-safe anyway - there's no
guarantee that a change to the value of busy on one thread will be seen
on another thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Thanks, how would you make to synchronize one function?
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Zürcher See <aq****@cannabismail.com> wrote:

<snip>
By implementing a very simple lock mechanism it work very well:

private bool busy=false;

public void EndFunction()
{
while(busy) System.Threading:Thread.Sleep(100);
busy=true;
...
busy=false;
}

Is that the only way to synchronize a bit of code without lock the entire
object?


No - just have lots of different locks, one for each thing you're
interested in. You don't have to use things like lock (this) - and I
generally argue that you shouldn't. I tend to make the lock private,
and a reference which nothing else will have.

Note that your code above isn't thread-safe anyway - there's no
guarantee that a change to the value of busy on one thread will be seen
on another thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Zürcher See <aq****@cannabismail.com> wrote:
Thanks, how would you make to synchronize one function?


Just use an object reference which is *only* used for synchronizing,
and *only* used for that particular method. I'd usually declare the
variable right beside the method to make that obvious:

object myMethodLock = new object();
void MyMethod (...)
{
lock (myMethodLock)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thank's, I was used to programm with java, when I said "Is that the only way
to synchronize ..." I meant "Where is the synchronize command in c#?"
Thank's for your help

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Zürcher See <aq****@cannabismail.com> wrote:
Thanks, how would you make to synchronize one function?


Just use an object reference which is *only* used for synchronizing,
and *only* used for that particular method. I'd usually declare the
variable right beside the method to make that obvious:

object myMethodLock = new object();
void MyMethod (...)
{
lock (myMethodLock)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Zürcher See <aq****@cannabismail.com> wrote:
Thank's, I was used to programm with java, when I said "Is that the only way
to synchronize ..." I meant "Where is the synchronize command in c#?"


Right. There's an attribute you can apply which is equivalent to the
synchronized method modifier in Java, but I wouldn't recommend it (or
using synchronized methods in Java, generally speaking). Using locks
which are hidden from the outside world makes the code a little bigger,
but more robust.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike | last post: by
2 posts views Thread by Fernando Rodríguez | last post: by
1 post views Thread by Jeff Gerber | last post: by
25 posts views Thread by Michael Kennedy | last post: by
6 posts views Thread by Clark Sann | last post: by
2 posts views Thread by www.brook | last post: by
12 posts views Thread by Perecli Manole | last post: by
2 posts views Thread by DeveloperX | last post: by
3 posts views Thread by shorti | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.