471,350 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

about concurent access : event/timer

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
Feb 4 '06 #1
3 1207
# 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>

Feb 4 '06 #2
# Cyrille37 # <cy*******@free.fr> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #3
Jon Skeet [C# MVP] a écrit :
# Cyrille37 # <cy*******@free.fr> wrote:

<snip>
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?


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.
Feb 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Kenneth Courville | last post: by
10 posts views Thread by Wayne Aprato | last post: by
3 posts views Thread by Tracey | last post: by
2 posts views Thread by Steve Richfie1d | last post: by

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.