473,381 Members | 1,431 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,381 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 1245
# 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kenneth Courville | last post by:
Hello, I'm looking for assistance with the Access object model. I know this is VB, but I'm building an Office Add-using C# directed at Access 2002. I'm literate in VB, so you can reply in VB... I...
13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
10
by: Wayne Aprato | last post by:
I have a database that was written in Access 2003 using the Access 2000 file format. When I run the mdb in Access 2003 everything works fine. If I run it in Access 2000 the timer event won't fire...
1
by: girlkordic | last post by:
I have a linked table from an Oracle database (that contains HR information for my company)in an Access database. The Oracle DB is updated on a daily basis. I would like one of my tables in...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
3
by: Tracey | last post by:
sorry I post this problem again. I have to stop my work to fix the problem. I'm doing a multi form application(Not a MDI one). My startup form say Form1 has a datagrid say datagrid1, when I...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
2
by: Steve Richfie1d | last post by:
In Access 97, in the Form_Current routine of a form, I call another routine that repopulates and recomputes everything. However, a SQL Select statement that I initiate from DAO that refers to a...
3
by: Curious | last post by:
Hi, I have a program that gets the prices of stocks in real-time. The program consists of the following key components: // Constructor public GetRealTimeStockPrice() { // Step 1
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.