Results 1 to 8 of 8

Thread: Timer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207

    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 myObjectEventArgs 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

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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 senderElapsedEventArgs e)
            {
                
    Console.WriteLine(e.SignalTime.ToString());
            }
        }


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    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

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    True, however I did find that example in the MSDN documentation describing how to add a timer to a console app.

    Jeremy

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i think the delegate doesnt match up:

    PHP Code:
    static void checkOnTimer(Object myObjectEventArgs myEventArgs)
            {
                
    Console.WriteLine(DateTime.Now.ToString());
            } 
    try using

    PHP Code:
            public static void TimeElapsed(object senderElapsedEventArgs e)
            {
                
    Console.WriteLine(e.SignalTime.ToString());
            } 
    \m/\m/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    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
  •  



Click Here to Expand Forum to Full Width