注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

分享,态度 ·~~

—— 十年太长,五年;如果可以回到五年前,你最想对那时候的自己说什么?

 
 
 

日志

 
 

C#:Wake up from Sleep (CreateWaitableTimer)  

2010-06-15 11:38:18|  分类: DotNet |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

This code isn't mine, but from a newsgroup posting on Eggheadcafe.com by MVP Willy Denoyette (I couldn't find a homepage to link). I'm reposting it here for my own benefit, safe in the knowledge it will never disappear unless my database backup corrupts.

The code illustrates how to set a timer to wake your computer up having been sleep'd. It's tested on Vista and Windows 7 with sleep mode. I haven't tried it with a laptop or hibernating however.

I'll be making it into a winforms in the future soon, that sits in the system tray - I'll update the page and make a google code project when this is done.

|++|++|++|++|+++|++|++|+|++|+|+|+|+|+|+|+++|+|+|++|+|+|++|+|+|++|+|+|+|+|++|+|++|+++|+|++++|++|+|+++|++|++|

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Threading;
using System.ComponentModel;
 
namespace ConsoleApplication1
{
class Program
{
[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);
 
static void Main(string[] args)
{
SetWaitForWakeUpTime();
}
 
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();
}
}
}
 
  评论这张
 
阅读(1279)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017