473,322 Members | 1,241 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,322 software developers and data experts.

timer

Hi Im using
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(onTimer);
myTimer.Interval = 1000;
myTimer.Start();
and getting error onTimer does not exist in the current context. Any
Ideas
Regards Robert

May 23 '06 #1
8 3528
I suppose this is im method body.
try to declare myTimer in class not in method body

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 23 '06 #2
Well, where /is/ onTimer?

Marc
May 23 '06 #3
try this

class Myclass: Form //probably
{
System.Timers.Timer myTimer;

private void button1_Click(object sender, EventArgs e)
{
myTimer = new System.Timers.Timer();
myTimer.Elapsed += new
ElapsedEventHandler(MyTimer_Elapsed);
timer1.Tick+=new EventHandler(timer1_Tick);
myTimer.Interval = 1000;

}
void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
}

hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 23 '06 #4
3 big problems with this suggestion:

1) System.Timers.Timer calls back on a different thread (requiring
additional handling)
2) You should dispose the Timer (either forms or system)
3) Because if you don't do 2 it keeps firing forever ;-p

Following illustrates this using 2 timers - one System.Timers and one
System.Windows.Forms; watch the debug output and see the threads being used;
also note that if you get rid of the Dispose override then it keeps firing
even after the end of the form - which can lead to the event trying to
access a disposed form.

Marc

using System;

static class Program {
public class MyForm : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components = null;
private System.Timers.Timer timer1;
private System.Windows.Forms.Timer timer2;
public MyForm() {
components = new System.ComponentModel.Container();
timer1 = new System.Timers.Timer(1000);
timer2 = new System.Windows.Forms.Timer(components);
timer2.Interval = 1000;
components.Add(timer1); // add timer1
timer1.Elapsed += Tick;
timer2.Tick += Tick;
timer1.Start();
timer2.Start();
}
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
private void Tick(object sender, EventArgs args) {
System.Threading.Thread thread =
System.Threading.Thread.CurrentThread;
System.Diagnostics.Debug.WriteLine(sender.GetType( ).FullName,
thread.ManagedThreadId.ToString() + "[" + (thread.IsThreadPoolThread ?
"(pool)" : thread.Name) + "]");
}

}
static void Main() {
System.Threading.Thread.CurrentThread.Name = "UI";
using (MyForm form = new MyForm()) {
form.ShowDialog();
}
System.Windows.Forms.MessageBox.Show("Done");
}

}
May 23 '06 #5
Thanks for all your replys. I have to admit this has gone beyond my
level of understanding. Ive not long started using c#. What Im trying
to do is every 30sec or so, is update a textbox with what number record
I on. From what ive read on the timer, it started of easy, but I cant
work out wether I need to create a class, then add to project, or use
within my program. Thanks again.
Regards Robert

May 23 '06 #6
you're right

Thank you

Regards,
Galin Iliev[MCSD.NET]
www.galcho.com

May 23 '06 #7
This is going to sound sarcastic, but I certainly don't mean it any way but
to be genuinely helpful. Bookmark the following in your browser:

http://msdn.microsoft.com/library/en...asp?frame=true

That is the URL of the online Microsoft .Net SDK, and it contains everything
you need to know. The reason I'm giving you such a general reference is due
to your statements that you've just started out using c#, and of course, the
nature of your question. Everyone starts out at the beginning, and mastery
of .Net programming is a long and difficult task.

It's one thing to understand the syntax of a programming language. It's
another thing altogether to understand the vast mountain of tools and
technologies that the .Net platform makes avaialable to you as a developer.
I've been a developer for more than a dozen years, and I still probably
spend a total of an hour a day with my head buried in the .Net SDK and other
resources.

..Net programming is a lot more than writing code. It is the employment of
Object-oriented, event-driven programming and design principles with a
starter kit of tens of thousands of ready-built classes to work with. I am
only familiar with about half of them at this point, and I've been using
them for about 5 or 6 years now. It's intimidating! But the SDK also
contains a large number of articles, tutorials, and sample code to help you
get through it. Start small, start from the beginning, and you might want to
start with some of the "Starter Kits" in the SDK. Otherwise, you'll drive
yourself crazy and likely become discouraged and give up. That would not be
a good thing. .Net programming is very rewarding in the long run! But even
for those of us who love it, it's always a "love-hate" relationship.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

<Ro********@yahoo.co.uk> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Thanks for all your replys. I have to admit this has gone beyond my
level of understanding. Ive not long started using c#. What Im trying
to do is every 30sec or so, is update a textbox with what number record
I on. From what ive read on the timer, it started of easy, but I cant
work out wether I need to create a class, then add to project, or use
within my program. Thanks again.
Regards Robert

May 23 '06 #8
On Tue, 23 May 2006 12:37:51 -0400, "Kevin Spencer"
<ke***@DIESPAMMERSDIEtakempis.com> wrote:
This is going to sound sarcastic, but I certainly don't mean it any way but
to be genuinely helpful. Bookmark the following in your browser:

http://msdn.microsoft.com/library/en...asp?frame=true

That is the URL of the online Microsoft .Net SDK, and it contains everything
you need to know. The reason I'm giving you such a general reference is due
to your statements that you've just started out using c#, and of course, the
nature of your question. Everyone starts out at the beginning, and mastery
of .Net programming is a long and difficult task.

It's one thing to understand the syntax of a programming language. It's
another thing altogether to understand the vast mountain of tools and
technologies that the .Net platform makes avaialable to you as a developer.
I've been a developer for more than a dozen years, and I still probably
spend a total of an hour a day with my head buried in the .Net SDK and other
resources.

.Net programming is a lot more than writing code. It is the employment of
Object-oriented, event-driven programming and design principles with a
starter kit of tens of thousands of ready-built classes to work with. I am
only familiar with about half of them at this point, and I've been using
them for about 5 or 6 years now. It's intimidating! But the SDK also
contains a large number of articles, tutorials, and sample code to help you
get through it. Start small, start from the beginning, and you might want to
start with some of the "Starter Kits" in the SDK. Otherwise, you'll drive
yourself crazy and likely become discouraged and give up. That would not be
a good thing. .Net programming is very rewarding in the long run! But even
for those of us who love it, it's always a "love-hate" relationship.

Very sound advice :)

Steve

http://www.pretty-vacant.co.uk
May 23 '06 #9

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

Similar topics

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...
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...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
12
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...
8
by: KnighT | last post by:
I have a .net service that runs a System.Threading.Timer. The delegate points to the function that the service should execute when the timer elapses. Problem: The timer is not ticking. I have...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
11
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.