473,498 Members | 1,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer question

Hi,
I am having a problem enabling a timer in my class. I have attached some
sample pseudo code (see "Sample code illustrating issue") so you can see
what I am talking about.

Whats the issue?
================
I am unable to pause the timer when a timed event is thrown due to my
event handling having a private scope. If I make the scope public all
works as expected.

I want to keep the scope private as I do not want this routine to be
accessible externally. I also think that I should be able to access this
as an object reference (ie object source) is provided.

How would I go about doing this? If I step into the code I see that
object source is of type System.Timers.Timer and that this is a
reference to my timer. I assume that I need to do some sort of late
binding to access it? How would I do this?

Sample code illustrating issue.
===============================
public class foo{
private System.Timers.Timer CamTimer = new System.Timers.Timer();

public foo()
{
// Initialization
CamTimer.Enabled = false;
CamTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
CamTimer.Interval = interval;
CamTimer.Enabled = true;
CamTimer.Start();
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Pause timer
// This is what is failing
CamTimer.Enabled = false

// Do some stuff

// Restart timer
}
}

Many thanks in advance for looking at this.

Regards

Benjamin
Nov 16 '05 #1
2 1702
Benjamin <cs************@spamgourmet.com> wrote:
I am having a problem enabling a timer in my class. I have attached some
sample pseudo code (see "Sample code illustrating issue") so you can see
what I am talking about.

Whats the issue?
================
I am unable to pause the timer when a timed event is thrown due to my
event handling having a private scope. If I make the scope public all
works as expected.

I want to keep the scope private as I do not want this routine to be
accessible externally. I also think that I should be able to access this
as an object reference (ie object source) is provided.

How would I go about doing this? If I step into the code I see that
object source is of type System.Timers.Timer and that this is a
reference to my timer. I assume that I need to do some sort of late
binding to access it? How would I do this?


Your OnTimedEvent method is static, whereas the variable is an instance
variable. That's the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] wrote:
Your OnTimedEvent method is static, whereas the variable is an instance
variable. That's the problem.


Thank you for such a quick response. You were quite correct. Looks like
I need to do some more reading...

Thanks again, much appreciated.
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3621
by: brian | last post by:
I have an ASP.Net application that uses impersonation. This works fine for accessing/executing the application. However, the app utilizes a timer, that when fired uses the <machine>ASPNET...
5
2115
by: Richard P | last post by:
I need some help on timers. My app is asp.net 1.1 website running in a shared hosting environment with a third-party service provider. I currently request and cache 20 - 40 remote RSS feeds. When a...
6
2888
by: Steve Jorgensen | last post by:
I know quite well that this question falls into the category of "why does Access misbehave when I do unexpected things to its objects?", but I thought I'd ask anyway, and see if anyone knows. ...
1
3270
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer...
10
1636
by: Bob | last post by:
Okay, I've done this for years but now I'm going to question it just because this idea has been at the back of my head since I started using DotNet... My WinForms app queries a database every 60...
7
5940
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
12
5490
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
11
2566
by: Hotrod2000 | last post by:
I'm quite new to programming but I'm having problems getting a timer to work in visual studio.net I've created a timer on a form, enabled it and then typed the following code (from the mdsn...
16
3226
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
11
4207
by: Anil Gupte/iCinema.com | last post by:
When I use this Dim instance As New Timer I get the error: Error 1 Overload resolution failed because no accessible 'New' accepts this number of arguments. Yet, in the help section for...
0
7124
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
7163
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,...
1
6884
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
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
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,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.