
Alarm clock for computer in C #
Surely many of us wanted our favorite computer to wake us up in the morning and “sing” our favorite songs (at least I wanted to), so I decided to write such an application as soon as I started learning programming.
How was it supposed to be? I launch my application - an alarm clock, set the number of hours it should wake me up and which program to run at the same time (telly, music ...) and put the PC to sleep mode, after the time the program should send the OS some kind of signal “Get up” it's time to wake up the owner! ”and run the application I need.
To do this, I shoveled the Internet floor, read many articles about the hibernation mode of the computer, which makes Windows OS at this moment, etc. asked questions in different forums and all they could tell me was “The alarm clock is on the phone, why do you need this hemorrhoids” or something like that, though they wrote on Microsoft what can be done but it will be rather difficult, in short they advised me to use the technology WOL there somehow it is all done through the network, while one computer must be constantly on to send a signal to another computer and that one should wake up! But it seemed to me that it would somehow not work out conveniently. But I didn’t despair and went towards WinAPI, and now the moment is true ... "Hooray, Found!".
For work I needed only three WinAPI functions and MSDN help.
So let's get started!
1. Create a console application;
2. Connect namespace to work with the API;
3. Write the necessary functions;
4. Create one method.
To work with WinAPI, connect System.Runtime.InteropServices and write our API functions
Next, create a method that will count down the time until waking up:
I hope everything is clear in the code, the long variable stores time (that is, the number of intervals of 100 nanoseconds), then we created a timer that processes all this and, after a time, starts the player.
Everything is so simple, but how much effort I put in to find how you can do it.
As a result, we got a useful application that can wake us up in the morning (plus everything, it turned out to be a beautiful interface).
It is certainly not very ergonomic, but everything is in your hands, you can transfer it to WinForms, add some more useful functions and you will get a full-fledged application.
I hope you are interested in reading my post, thanks for your attention!
PS If the application does not work for you but the compilation was error-free, then check if the option "Allow wake-up timers" is enabled in your power settings.
How was it supposed to be? I launch my application - an alarm clock, set the number of hours it should wake me up and which program to run at the same time (telly, music ...) and put the PC to sleep mode, after the time the program should send the OS some kind of signal “Get up” it's time to wake up the owner! ”and run the application I need.
To do this, I shoveled the Internet floor, read many articles about the hibernation mode of the computer, which makes Windows OS at this moment, etc. asked questions in different forums and all they could tell me was “The alarm clock is on the phone, why do you need this hemorrhoids” or something like that, though they wrote on Microsoft what can be done but it will be rather difficult, in short they advised me to use the technology WOL there somehow it is all done through the network, while one computer must be constantly on to send a signal to another computer and that one should wake up! But it seemed to me that it would somehow not work out conveniently. But I didn’t despair and went towards WinAPI, and now the moment is true ... "Hooray, Found!".
For work I needed only three WinAPI functions and MSDN help.
So let's get started!
1. Create a console application;
2. Connect namespace to work with the API;
3. Write the necessary functions;
4. Create one method.
To work with WinAPI, connect System.Runtime.InteropServices and write our API functions
[DllImport("kernel32.dll")]
public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll")]
public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
public static extern Int32 WaitForSingleObject(IntPtr handle, uint
milliseconds);
Next, create a method that will count down the time until waking up:
static IntPtr handle;
static void SetWaitForWakeUpTime()
{
long duetime = -900000000;
Console.WriteLine("Все, ложитесь спать хозяин! Я Вас утром разбужу!);
handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
uint INFINITE = 0xFFFFFFFF;
int ret = WaitForSingleObject(handle, INFINITE);
System.Diagnostics.Process.Start("C:\Program Files\AIMP3\AIMP3.exe");
}
I hope everything is clear in the code, the long variable stores time (that is, the number of intervals of 100 nanoseconds), then we created a timer that processes all this and, after a time, starts the player.
Everything is so simple, but how much effort I put in to find how you can do it.
As a result, we got a useful application that can wake us up in the morning (plus everything, it turned out to be a beautiful interface).
It is certainly not very ergonomic, but everything is in your hands, you can transfer it to WinForms, add some more useful functions and you will get a full-fledged application.
I hope you are interested in reading my post, thanks for your attention!
PS If the application does not work for you but the compilation was error-free, then check if the option "Allow wake-up timers" is enabled in your power settings.