Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to customize timer to fire my routine at specific time

How to customize timer to fire my routine at specific time

Scheduled Pinned Locked Moved C#
helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    i am using this code

    using System;
    using System.Windows.Forms;
    using System.Timers;

    namespace T_TEST
    {
    public partial class Form1 : Form
    {
    static System.Timers.Timer timer;
    public Form1()
    {
    InitializeComponent();
    }

        private void Form1\_Load(object sender, EventArgs e)
        {
            schedule\_Timer();
        }
    
        static void schedule\_Timer()
        {
            Console.WriteLine("### Timer Started ###");
    
            DateTime nowTime = DateTime.Now;
            DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\]
            if (nowTime > scheduledTime)
            {
                scheduledTime = scheduledTime.AddDays(1);
            }
    
            double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
            timer = new System.Timers.Timer(tickTime);
            timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed);
            timer.Start();
        }
    
        static void timer\_Elapsed(object sender, ElapsedEventArgs e)
        {
            //Console.WriteLine("### Timer Stopped ### \\n");
            timer.Stop();
            MessageBox.Show("Hello World!!!");
            //Console.WriteLine("### Scheduled Task Started ### \\n\\n");
            //Console.WriteLine("Hello World!!! - Performing scheduled task\\n");
            //Console.WriteLine("### Task Finished ### \\n\\n");
            schedule\_Timer();
        }
    }
    

    }

    the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks

    tbhattacharjee

    P OriginalGriffO 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      i am using this code

      using System;
      using System.Windows.Forms;
      using System.Timers;

      namespace T_TEST
      {
      public partial class Form1 : Form
      {
      static System.Timers.Timer timer;
      public Form1()
      {
      InitializeComponent();
      }

          private void Form1\_Load(object sender, EventArgs e)
          {
              schedule\_Timer();
          }
      
          static void schedule\_Timer()
          {
              Console.WriteLine("### Timer Started ###");
      
              DateTime nowTime = DateTime.Now;
              DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\]
              if (nowTime > scheduledTime)
              {
                  scheduledTime = scheduledTime.AddDays(1);
              }
      
              double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
              timer = new System.Timers.Timer(tickTime);
              timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed);
              timer.Start();
          }
      
          static void timer\_Elapsed(object sender, ElapsedEventArgs e)
          {
              //Console.WriteLine("### Timer Stopped ### \\n");
              timer.Stop();
              MessageBox.Show("Hello World!!!");
              //Console.WriteLine("### Scheduled Task Started ### \\n\\n");
              //Console.WriteLine("Hello World!!! - Performing scheduled task\\n");
              //Console.WriteLine("### Task Finished ### \\n\\n");
              schedule\_Timer();
          }
      }
      

      }

      the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks

      tbhattacharjee

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You should look at this[^] - it's one of the more obscure events I grant you, but it should be easy enough to hook into this.

      This space for rent

      1 Reply Last reply
      0
      • T Tridip Bhattacharjee

        i am using this code

        using System;
        using System.Windows.Forms;
        using System.Timers;

        namespace T_TEST
        {
        public partial class Form1 : Form
        {
        static System.Timers.Timer timer;
        public Form1()
        {
        InitializeComponent();
        }

            private void Form1\_Load(object sender, EventArgs e)
            {
                schedule\_Timer();
            }
        
            static void schedule\_Timer()
            {
                Console.WriteLine("### Timer Started ###");
        
                DateTime nowTime = DateTime.Now;
                DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 1, 59, 0, 0); //Specify your scheduled time HH,MM,SS \[1am and 59 minutes\]
                if (nowTime > scheduledTime)
                {
                    scheduledTime = scheduledTime.AddDays(1);
                }
        
                double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
                timer = new System.Timers.Timer(tickTime);
                timer.Elapsed += new ElapsedEventHandler(timer\_Elapsed);
                timer.Start();
            }
        
            static void timer\_Elapsed(object sender, ElapsedEventArgs e)
            {
                //Console.WriteLine("### Timer Stopped ### \\n");
                timer.Stop();
                MessageBox.Show("Hello World!!!");
                //Console.WriteLine("### Scheduled Task Started ### \\n\\n");
                //Console.WriteLine("Hello World!!! - Performing scheduled task\\n");
                //Console.WriteLine("### Task Finished ### \\n\\n");
                schedule\_Timer();
            }
        }
        

        }

        the above code has one problem that once the timer fire then it will fire next day. i want if timer fire and after few minute if i reset my system time again then it should fire again. how to customize the above code for my requirement. anyone mind to help me plzzz. thanks

        tbhattacharjee

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Because you set the tick time to a specific value, means that it fires at that interval, not that it fires at a specific time: it doesn't add the tick interval to the system clock when you start the timer, it sets a specific number of ticks that should elapse before the timer event is fired. Changing the system clock does not affect that tick count, so if you set a timer for one hour and then move the clock forward 59 minutes, it will still be an hour before the timer fires. If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        T 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Because you set the tick time to a specific value, means that it fires at that interval, not that it fires at a specific time: it doesn't add the tick interval to the system clock when you start the timer, it sets a specific number of ticks that should elapse before the timer event is fired. Changing the system clock does not affect that tick count, so if you set a timer for one hour and then move the clock forward 59 minutes, it will still be an hour before the timer fires. If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          T Offline
          T Offline
          Tridip Bhattacharjee
          wrote on last edited by
          #4

          can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks

          tbhattacharjee

          OriginalGriffO R 2 Replies Last reply
          0
          • T Tridip Bhattacharjee

            can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks

            tbhattacharjee

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You should be able to do that yourself, if you wrote the code you show! You know how to set the interval on the timer, and you know how to create variables...

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • T Tridip Bhattacharjee

              can u plzz post some sample what u said "If you want to first at a time, then set the timer interval to 30 seconds, and set a class level DateTime value at which you want to call your method. In the tick handler, compare the current time to the class level value and call your method if it matches. That way, you can reset the clock after it fires, and it will match a second time." thanks

              tbhattacharjee

              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #6

              - call your timer (for example) every Minute - set timer.tick to 60000 - when you have the timer.event don't stop the timer - compare if now is greater or equal your schedule-time, but less your schedule-time +30 seconds - if yes the give the message and increase your schedule-time

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups