about concurent access : event/timer

C

# Cyrille37 #

Hello,

I've got a method which could be called by a UI Event and a Timer.
I would like to be shure that not 2 concurents calls append.


I don't know if I can use System.Threading.Monitor.
And if System.Threading.Monitor is the right method, which object I've to
Monitoring ?

Here is a peace of code that should show the context.

Can you help me by telling me where and how to put the critical section.

<code>
UserControl userControl1 ;
Timer timer1 ;

....

private void userControl1_VisibleChanged(object sender, System.EventArgs e)
{
if( this.Visible )
{
return ;
}
// If it comes invisible, send the message.
SendMsg();
// timer is not active while the form is not visible.
this.timer1.Stop();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
// every N ticks, send the message
SendMsg();
}

// here should be the critical section
void SendMsg()
{
this.timer1.Stop();
// ... send the message
this.timer1.Start();
}

</code>

Thanks for your help.
cyrille
 
C

# Cyrille37 #

# Cyrille37 # a écrit :
Hello,

I've got a method which could be called by a UI Event and a Timer.
I would like to be shure that not 2 concurents calls append.


I don't know if I can use System.Threading.Monitor.
And if System.Threading.Monitor is the right method, which object I've
to Monitoring ?

Here is a peace of code that should show the context.

Can you help me by telling me where and how to put the critical section.

<code>
UserControl userControl1 ;
Timer timer1 ;

....

private void userControl1_VisibleChanged(object sender, System.EventArgs e)
{
if( this.Visible )
{
return ;
}
// If it comes invisible, send the message.
SendMsg();
// timer is not active while the form is not visible.
this.timer1.Stop();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
// every N ticks, send the message
SendMsg();
}

// here should be the critical section
void SendMsg()
{
this.timer1.Stop();
// ... send the message
this.timer1.Start();
}

</code>

Thanks for your help.
cyrille

Is that the right solution ???

<code>
object dummyMonitorObject = new object() ;
...
void SendMsg()
{
this.timer1.Stop();
if( ! Monitor.TryEnter(dummyMonitorObject) )
{
return ;
}
// ... send the message
Monitor.Exit( dummyMonitorObject );
this.timer1.Start();
}
</code>
 
J

Jon Skeet [C# MVP]

Is that the right solution ???

<code>
object dummyMonitorObject = new object() ;
...
void SendMsg()
{
this.timer1.Stop();
if( ! Monitor.TryEnter(dummyMonitorObject) )
{
return ;
}
// ... send the message
Monitor.Exit( dummyMonitorObject );
this.timer1.Start();
}
</code>

Do you really want it to not do anything if another thread is already
executing? If so, that's okay - although you should put the call to
Exit in a finally block so that even if your message sending code
throws an exception, you still unlock the monitor.
 
C

# Cyrille37 #

Jon Skeet [C# MVP] a écrit :
Do you really want it to not do anything if another thread is already
executing?

Yes, shure.
There is a Richtextbox where user put some text.
I send that text automatically every one minute and if the control becomeinvisible.
If so, that's okay - although you should put the call to
Exit in a finally block so that even if your message sending code
throws an exception, you still unlock the monitor.

Thanks for the tip !

Regards,
Cyrille.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top