Surely, many of us wanted us to wake up our beloved computer in the morning and “sing” our favorite songs (at least I wanted it so), the poet decided to write such an application as soon as he began to study programming.
How was it supposed to be? I launch my application - an alarm clock, set it up at how many hours it should wake me up and which program should I run (TV, music ...) and put my PC into sleep mode, after the time the program had to give the OS some kind of “Wake up” signal. it's time to wake the host! ”and launch the application I need.
To do this, I shoveled the Internet, read a lot of articles about computer hibernation, what Windows is doing at this point, etc. asked questions in different forums and all they could say to me was “The alarm clock is on the phone, why do you need this hemorrhoids” or what is that kind of thing, though they wrote on Microsoft that what can be done, but it will be pretty difficult, in short, they advised me to use the technology
WOL there as it is all done through the network, with one computer must be constantly turned on to give a signal to another computer and he must wake up! But it seemed to me that this is somehow inconvenient. But nothing, I did not despair and went in the direction of WinAPI, and now the moment is true ... "Hurray, Found!".
I only needed three WinAPI functions and MSDN help to work.
So let's get started!
1. Create a console application;
2. Connect the namespace to work with the API;
3. Write the necessary functions;
4. Create one method.
')
To work with WinAPI, we connect the
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 time to wake 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 of these and after the time expires, the player starts.
Everything is so simple, but how much effort is put to find how to do it.
As a result, we got a useful application that can wake us up in the morning (plus everything 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 a few more useful features and you will have a full application.
I hope you are interested in reading my post, thank you for your attention!
PS If the application does not work for you but the compilation went without errors, then check whether the item “Allow wake-up timers” is enabled in your power settings.