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. Time EventHandler

Time EventHandler

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 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.
  • B Offline
    B Offline
    brunoconde
    wrote on last edited by
    #1

    I'm making a program to schedule my programs and to shut down my computer in a choosen time but this way my processor is 100 percent, logicaly. How can i change this. while (!(System.DateTime.Now.Equals(shut.getDateTime()))) { } this.Computer_Shutdown(); Please help me. Thank you. :((

    S M 2 Replies Last reply
    0
    • B brunoconde

      I'm making a program to schedule my programs and to shut down my computer in a choosen time but this way my processor is 100 percent, logicaly. How can i change this. while (!(System.DateTime.Now.Equals(shut.getDateTime()))) { } this.Computer_Shutdown(); Please help me. Thank you. :((

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Add a Thread.Sleep(1) into your while loop and cpu usage will drop :) Otherwise you could use a timer to periodically process the following:

      if (System.DateTime.Now.Equals(shut.getDateTime()))
      this.Computer_Shutdown();


      www.troschuetz.de

      1 Reply Last reply
      0
      • B brunoconde

        I'm making a program to schedule my programs and to shut down my computer in a choosen time but this way my processor is 100 percent, logicaly. How can i change this. while (!(System.DateTime.Now.Equals(shut.getDateTime()))) { } this.Computer_Shutdown(); Please help me. Thank you. :((

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Using such a busy wait is _really_ bad style. I'd suggest the following: Create a new timer and set it's interval to the amount of milliseconds from now to the desired shutdown time:

        Timer shutTimer = new Timer();
        shutTimer.Tick += new EventHandler(shutTimer_Tick);
        TimeSpan waitDuration = shut.GetDateTime().Subtract(DateTime.Now);
        shutTimer.Interval = (int)waitDuration.TotalMilliseconds;
        shutTimer.Start();
        [...]
        private void shutTimer_Tick(object sender, EventArgs e)
        {
        this.Computer_Shutdown();
        }

        Regards, mav

        B 1 Reply Last reply
        0
        • M mav northwind

          Using such a busy wait is _really_ bad style. I'd suggest the following: Create a new timer and set it's interval to the amount of milliseconds from now to the desired shutdown time:

          Timer shutTimer = new Timer();
          shutTimer.Tick += new EventHandler(shutTimer_Tick);
          TimeSpan waitDuration = shut.GetDateTime().Subtract(DateTime.Now);
          shutTimer.Interval = (int)waitDuration.TotalMilliseconds;
          shutTimer.Start();
          [...]
          private void shutTimer_Tick(object sender, EventArgs e)
          {
          this.Computer_Shutdown();
          }

          Regards, mav

          B Offline
          B Offline
          brunoconde
          wrote on last edited by
          #4

          But I can't use a timer because I'm using a thread to do this, i tried and did not work. The idea is great. Thanks. ;)

          M 1 Reply Last reply
          0
          • B brunoconde

            But I can't use a timer because I'm using a thread to do this, i tried and did not work. The idea is great. Thanks. ;)

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            That's why there are several different types of timers in .NET. If you can't use a System.Windows.Forms.Timer (because you don't have a GUI) then a System.Timers.Timer should work fine. The events differ a little, but the concept stays the same: Calculate how long until the desired shutdown time and wind up the timer... Regards, mav

            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