473,399 Members | 4,192 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,399 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 1854
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 Event Monitor Log (DEADLOCKS WITH DETAILS) here....
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. For example: (and this seems consistent) In list...
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 thing I don't understand. Why do have to pass a...
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 as expected. I have found no explaination in...
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, which I think has uncovered a serious error in the...
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...
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 threads. Thread 1 does a Monitor.Wait in a SyncLock...
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 question(s) comes in. In the original it seems to work...
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 the options. It doesnt give me the detailed...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.