473,383 Members | 1,735 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,383 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 3530
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...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.