none
Multithreading in c# RRS feed

  • Question

  • Hello,

    I have an application in which i want to create a background process which will run until the application gets closed.

    This background process should monitor the current time and if current time matches with specific time then i want to do some processing.

    so how to do that?

    Tuesday, April 13, 2010 5:11 AM

Answers

  •         System.DateTime targetTime = new DateTime(2010, 4, 13, 1, 5, 00);
            System.Timers.Timer timerTimeTrigger;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                timerTimeTrigger = new System.Timers.Timer() { AutoReset = false };
                timerTimeTrigger.Elapsed += new System.Timers.ElapsedEventHandler(timerTimeTrigger_Elapsed);
                Microsoft.Win32.SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
                Console.WriteLine(targetTime.ToLongTimeString());
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                if (targetTime > DateTime.Now)
                {   timerTimeTrigger.Interval = targetTime.Subtract(DateTime.Now).TotalMilliseconds;
                    timerTimeTrigger.Start();   }
            }
    
            void timerTimeTrigger_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.WriteLine("Target Time Hit: " + DateTime.Now.ToLongTimeString());
            }
            void SystemEvents_TimeChanged(object sender, EventArgs e)
            {
                if (targetTime > DateTime.Now)
                {   timerTimeTrigger.Interval = targetTime.Subtract(DateTime.Now).TotalMilliseconds;
                    timerTimeTrigger.Start();   }
            }
    Since you know what time it is now, and also what time you want to execute - calculating the time until the trigger happens makes more sense than rechecking over and over (unless time is passing at variable increments; then we're all in trouble).  This snippet is pretty straight forward.  Find out how long to wait and wait for that long.  If the system's time changes for whatever reason; recalculate the time to wait.
    Tuesday, April 13, 2010 6:15 AM

All replies

  •         System.DateTime targetTime = new DateTime(2010, 4, 13, 1, 5, 00);
            System.Timers.Timer timerTimeTrigger;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                timerTimeTrigger = new System.Timers.Timer() { AutoReset = false };
                timerTimeTrigger.Elapsed += new System.Timers.ElapsedEventHandler(timerTimeTrigger_Elapsed);
                Microsoft.Win32.SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
                Console.WriteLine(targetTime.ToLongTimeString());
                Console.WriteLine(DateTime.Now.ToLongTimeString());
                if (targetTime > DateTime.Now)
                {   timerTimeTrigger.Interval = targetTime.Subtract(DateTime.Now).TotalMilliseconds;
                    timerTimeTrigger.Start();   }
            }
    
            void timerTimeTrigger_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.WriteLine("Target Time Hit: " + DateTime.Now.ToLongTimeString());
            }
            void SystemEvents_TimeChanged(object sender, EventArgs e)
            {
                if (targetTime > DateTime.Now)
                {   timerTimeTrigger.Interval = targetTime.Subtract(DateTime.Now).TotalMilliseconds;
                    timerTimeTrigger.Start();   }
            }
    Since you know what time it is now, and also what time you want to execute - calculating the time until the trigger happens makes more sense than rechecking over and over (unless time is passing at variable increments; then we're all in trouble).  This snippet is pretty straight forward.  Find out how long to wait and wait for that long.  If the system's time changes for whatever reason; recalculate the time to wait.
    Tuesday, April 13, 2010 6:15 AM
  • You can omit the 2 console.writelines in the form_load.  They're for debug purposes.
    Tuesday, April 13, 2010 6:17 AM
  • Thank you! Its working fine for me..
    Tuesday, April 13, 2010 10:33 AM