|
-
Dec 22nd, 2002, 04:19 PM
#1
Thread Starter
Addicted Member
Timer
I have to following code:
PHP Code:
System.Windows.Forms.Timer onCheckTimer = new System.Windows.Forms.Timer();
onCheckTimer.Tick += new EventHandler(checkOnTimer);
Console.WriteLine("Startin TImer...");
onCheckTimer.Interval = 1000;
onCheckTimer.Start();
PHP Code:
static void checkOnTimer(Object myObject, EventArgs myEventArgs)
{
Console.WriteLine(DateTime.Now.ToString());
}
My question is does anyone know why the timer event never gets raised. I took it from the msdn documentation but can't seem to get it work work. The first part of the code is from a console app called and the function would be main.
If anyone has any insight then I would be exteremly greatfull.
Jeremy
-
Dec 22nd, 2002, 07:15 PM
#2
Hyperactive Member
not sure why that doesn't work, probably needs a container(like a form) or something. This works though, just catch the Elapsed event handler:
PHP Code:
using System;
using System.Timers;
namespace TimeTest
{
public class MainApp
{
public static void Main(string[] args)
{
Timer tmr = new Timer(1000);
tmr.Elapsed += new ElapsedEventHandler(TimeElapsed);
tmr.Start();
Console.ReadLine();
}
public static void TimeElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine(e.SignalTime.ToString());
}
}
}
-
Dec 22nd, 2002, 07:48 PM
#3
Thread Starter
Addicted Member
Thanks.
Not sure why the first code example did not work, you would think that they would make sure the code out of the documentation would do what is was supposed to.
Jeremy
-
Dec 22nd, 2002, 09:24 PM
#4
PowerPoster
You were using a windows form control. You need to use the generic timer class if you are doing a console app. I don't know why one is different than the other, but there is something there that won't allow it to work.
-
Dec 23rd, 2002, 12:44 AM
#5
Thread Starter
Addicted Member
True, however I did find that example in the MSDN documentation describing how to add a timer to a console app.
Jeremy
-
Dec 25th, 2002, 07:56 PM
#6
yay gay
i think the delegate doesnt match up:
PHP Code:
static void checkOnTimer(Object myObject, EventArgs myEventArgs)
{
Console.WriteLine(DateTime.Now.ToString());
}
try using
PHP Code:
public static void TimeElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine(e.SignalTime.ToString());
}
\m/  \m/
-
Dec 27th, 2002, 06:05 AM
#7
Watch out, by using the System.Timers.Timer object the elapsed event is in a different thread than the main app. Caught me when I was trying to manipulate a form from within the event.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 27th, 2002, 09:51 AM
#8
Thread Starter
Addicted Member
Actually I prefer to have it in a different thread anyways becaues all that it is doing is starting a thread.
Jeremy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|