Question

I have made a C# alarm clock and it's working fine. the problem is that when it runs it consumes 20% of the processor (on an i5 2410M processor) what should I do? here is my code:

using System;
namespace assigment1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime uptime = new DateTime (2013,12,10,4,0,0);
            Console.WriteLine("This alarm is set to go off at 4:00 am");
            while (true)
            {

                if (DateTime.Now.Minute == uptime.Minute && DateTime.Now.Hour == uptime.Hour)
                {
                    for (int j = 1000; j < 22767; j++)
                     {


                        Console.Beep(j, 500);
                        Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
                     }

                }
             }
        }
    }
}
Était-ce utile?

La solution 2

You need to calculate the time till till the alarm should beep and use the timer class. Just set the interval to the time remaining till alarm and stop the timer after that. Something like this should work

DateTime alarmTime = new DateTime(2013,12,10,4,0,0);
System.Windows.Forms.Timer alarmTimer = new System.Windows.Forms.Timer();
alarmTimer.Interval = (alarmTime - DateTime.Now).Milliseconds;
alarmTimer.Tick += alarmTimer_Tick;
alarmTimer.Start();

your event

void alarmTimer_Tick(object sender, EventArgs e)
{
    alarmTimer.Stop();
    Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
}

Autres conseils

This is because your while loop is running continuously without any break. Add a Thread.Sleep. This will add a pause in between checks and greatly increase your performance:

class Program
{
    static void Main(string[] args)
    {
        DateTime uptime = new DateTime (2013,12,10,4,0,0);
        Console.WriteLine("This alarm is set to go off at 4:00 am");
        while (true)
        {

            if (DateTime.Now.Minute == uptime.Minute && DateTime.Now.Hour == uptime.Hour)
            {
                for (int j = 1000; j < 22767; j++)
                 {


                    Console.Beep(j, 500);
                    Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
                 }                     

            }

            Thread.Sleep(1500); // Sleep 1.5 seconds.
         }
    }
}

if you want an alarm clock why you don't use Timer Class

I don't know if you can do that, but you can change the thread priority of the executing thread via the Priority property. You may want to try the following:

Thread.CurrentThread.Priority = ThreadPriority.Lowest;

Also, I don't think you really want to cap it. If the machine is otherwise idle, you'd like it to get busy on with the task, right? ThreadPriority helps communicate this to the scheduler.

You are putting the check within a while loop, which means it will be utilising a large proportion of your processor time.

I would suggest having a look at this article (http://www.infolet.org/2012/11/create-digital-clock-on-c-sharp-program-code.html) which describes how to do this using the Timer Class.

UPDATE: This SO answer is pretty nice and may be more suited if you're happy to use events; https://stackoverflow.com/a/1493235/465404

I think you should definitely be using a Timer class for your alarm and just change the tick interval accordingly. This will easily allow you to manage recurrence of the alarm as well.

So you're interval will be the difference in time between when the alarm is set and when you want it to go off.

I have used multiple of these running concurrently in a Win Forms app with very small resource utilisation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top