473,785 Members | 3,157 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.T imer 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.T imer CamTimer = new System.Timers.T imer();

public foo()
{
// Initialization
CamTimer.Enable d = false;
CamTimer.Elapse d+=new ElapsedEventHan dler(OnTimedEve nt);
CamTimer.Interv al = interval;
CamTimer.Enable d = true;
CamTimer.Start( );
}

private static void OnTimedEvent(ob ject source, ElapsedEventArg s e)
{
// Pause timer
// This is what is failing
CamTimer.Enable d = false

// Do some stuff

// Restart timer
}
}

Many thanks in advance for looking at this.

Regards

Benjamin
Nov 16 '05 #1
2 1725
Benjamin <cs************ @spamgourmet.co m> 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.T imer 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.co m>
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
3644
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 identity. In order to correct this problem I've modified the Machine.config file in the \Microsoft.Net\Framework\v1.1.4322 folder. However, this didn't solve the problem; instead of using the user id and password specified in Machine.Config, the...
5
2142
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 user requests the page, the app first tries to retrieve a feed from cache, if the feed has expired, it goes off and request the file from the web. If create a CacheItemRemovedCallback for each item to automatically re-request an expired...
6
2912
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. When I make calls to SetWindowLong and SetLayeredWindowAttributes using the window handle for a poup form in Access, and call from within a timer event handler, it works most of the time, but sometimes will stop the timer from firing until I click...
1
3300
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 objects which all fire into my OnTimer( object state ) function very nicely. I pass in an object "o" on creation of this timer which I subsequently get passed to me in my OnTimer function. Now in the OnTimer function I want to modify the object...
10
1662
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 seconds. I set up a singleton class that's instantiated when the app starts, and that starts a timer. My question is, would it be better to run this checking process on a separate thread and have it use its own DB connection? If there isn't a...
7
6083
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 seems recently connections just drop off because the timer stops firing. My question is if there is a timeout in the timer event that just shuts down the call if the timer event is taking too long to complete...?
12
5535
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 directories and updating a database. No interaction with the GUI. Problem is that the system timers do not have a tag property so I can tie in an object. example (old way):
11
2607
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 library as I thought this would be a good start!!!) but nothing happens :- Imports System.Timers
16
3257
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 do it. If I bring this same code outside this event handler, it works just fine. Is this normal?
11
4239
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 Timer (in VB 2005) this is exactly the syntax shown. I also tried:
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
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.