English 中文(简体)
WPF 应用程序: 将计算机从睡眠或休眠中唤醒
原标题:WPF Application: Wake computer from sleep or hibernation

最后一天我试图使这个工作:用WPF应用程序从睡眠或冬眠中唤醒我的电脑。我尝试过的任何办法都没有奏效。

到目前为止,我在网上尝试了最流行的例子。例如:

[DllImport("kernel32.dll")]
        public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, 
                                                                  bool bManualReset,
                                                                string lpTimerName);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, 
                                                    [In] ref long pDueTime, 
                                                              int lPeriod,
                                                           IntPtr pfnCompletionRoutine, 
                                                           IntPtr lpArgToCompletionRoutine, 
                                                             bool fResume);


        public static void SetWaitForWakeUpTime()
        {
            DateTime utc = DateTime.Now.AddMinutes(2);
            long duetime = utc.ToFileTime();

            using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer"))
            {
                if (SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true))
                {
                    using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
                    {
                        wh.SafeWaitHandle = handle;
                        wh.WaitOne();
                    时 时
                时 时
                else
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                时 时
            时 时

            // You could make it a recursive call here, setting it to 1 hours time or similar
            Console.WriteLine("Wake up call");
            Console.ReadLine();
        时 时

和用法:

WakeUp.SetWaitForWakeUpTime();

我也尝试过这个例子(看看我如何使用代码之后的方法):

public event EventHandler Woken;

private BackgroundWorker bgWorker = new BackgroundWorker();

public WakeUp()
{
    bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
    bgWorker.RunWorkerCompleted += 
      new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
时 时

public void SetWakeUpTime(DateTime time)
{
    bgWorker.RunWorkerAsync(time.ToFileTime());
时 时

void bgWorker_RunWorkerCompleted(object sender, 
              RunWorkerCompletedEventArgs e)
{
    if (Woken != null)
    {
        Woken(this, new EventArgs());
    时 时
时 时

private void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
{
    long waketime = (long)e.Argument;

    using (SafeWaitHandle handle = 
              CreateWaitableTimer(IntPtr.Zero, true, 
              this.GetType().Assembly.GetName().Name.ToString() + "Timer"))
    {
        if (SetWaitableTimer(handle, ref waketime, 0, 
                               IntPtr.Zero, IntPtr.Zero, true))
        {
            using (EventWaitHandle wh = new EventWaitHandle(false, 
                                                   EventResetMode.AutoReset))
            {
                wh.SafeWaitHandle = handle;
                wh.WaitOne();
            时 时
        时 时
        else
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        时 时
    时 时
时 时

时 时

和用法:

WakeUp w = new WakeUp();
                w.Woken += new EventHandler(w_Woken);
                w.SetWakeUpTime(alarmDate.Subtract(new TimeSpan(0,0,0,20)));

似乎没什么能唤醒我的电脑

我知道这是可能的,正如其他几个闹钟可以做到的那样。他们设法使我的电脑醒过来,没有问题,所以一定有错误。

最佳回答

问题并不在于所有计算机都支持这一问题。 如果您的计算机不支持,显然没有什么可做的。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签