472,353 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1806
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the...
2
by: Mike | last post by:
Hellos again, I seem to be having a weird issue. Whenever DB2 Health monitor seems to run, all connections to the database seem to just lock up....
2
by: Fernando Rodríguez | last post by:
Hi, I've been reading the multithreading tutorial at http://www.yoda.arachsys.com/csharp/threads/locking.shtml (thanks Jon :-) and there's one...
1
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...
25
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,...
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...
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...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3...
2
by: DeveloperX | last post by:
Hi, Below is a hacked up version of an example I found on MSDN. My only change was to add a third thread and some Sleeps. That's where my...
3
by: shorti | last post by:
Hello all, I am running with DB2 UDB V8 on AIX. I am trying to track down what appears to be a lock issue. I turned EVENT MONITOR on with all...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.