Monitor, Lock

  • Thread starter Thread starter Zürcher See
  • Start date Start date
Z

Zürcher See

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?
 
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.
 
Thanks, how would you make to synchronize one function?

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.
 
Zürcher See said:
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)
{
...
}
}
 
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

Zürcher See said:
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)
{
...
}
}
 
Zürcher See said:
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.
 
Back
Top