安排机器唤醒

发布于 2024-07-27 17:10:19 字数 254 浏览 12 评论 0原文

以编程方式使 Windows XP(或更高版本)计算机在特定时间唤醒的最佳方法是什么? (理想情况下,很像 Media Center 如何自动启动来录制特定的电视节目)

我有一个 Windows 服务(用 C# 编写),并且我希望该服务能够使其托管的计算机在预定时间启动。

是否需要配置任何 BIOS 设置或先决条件(例如 ACPI)才能使其正常工作?

该机器将使用拨号或 3G 无线调制解调器,因此不幸的是它不能依赖 LAN 唤醒。

What's the best way to programmatically cause a Windows XP (or above) machine to wake up at a specific time. (Ideally a lot like how Media Center can start up automatically to record a particular TV program)

I've got a Windows service (written in C#) and I'd like this service to be able to cause the machine it is hosted on to start up at predetermined times.

Are there any BIOS settings or prerequisites (eg. ACPI) that need to be configured for this to work correctly?

This machine would be using dialup or 3G wireless modem, so unfortunately it can't rely on Wake on LAN.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷不放纵 2024-08-03 17:10:19

您可以使用可等待计时器 从挂起或休眠状态唤醒。 据我所知,无法以编程方式从正常关闭模式(软关闭/S5)唤醒,在这种情况下,您需要在 BIOS 中指定 WakeOnRTC 警报。 要使用 C# 中的可等待计时器,您需要 pInvoke。 导入声明为:

public delegate void TimerCompleteDelegate();

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

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CancelWaitableTimer(IntPtr hTimer);

您可以按以下方式使用这些函数:

public static IntPtr SetWakeAt(DateTime dt)
{
    TimerCompleteDelegate timerComplete = null;

    // read the manual for SetWaitableTimer to understand how this number is interpreted.
    long interval = dt.ToFileTimeUtc(); 
    IntPtr handle = CreateWaitableTimer(IntPtr.Zero, true, "WaitableTimer");
    SetWaitableTimer(handle, ref interval, 0, timerComplete, IntPtr.Zero, true);
    return handle;
}

然后,您可以使用返回的句柄作为参数,使用 CancelWaitableTimer 取消可等待计时器。

您的程序可以使用 pInvoke 休眠和睡眠:

[DllImport("powrprof.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent);

public static bool Hibernate()
{
    return SetSuspendState(true, false, false);
}

public static bool Sleep()
{
    return SetSuspendState(false, false, false);
}

您的系统可能不允许程序让计算机进入休眠状态。 您可以调用以下方法来允许休眠:

public static bool EnableHibernate()
{
    Process p = new Process();
    p.StartInfo.FileName = "powercfg.exe";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.Arguments = "/hibernate on"; // this might be different in other locales
    return p.Start();
}

You can use waitable timers to wake from a suspend or hibernate state. From what I can find, it is not possible to programmatically wake from normal shut down mode (soft off/S5), in that case, you need to specify a WakeOnRTC alarm in BIOS. To use waitable timers from C#, you need pInvoke. The import declarations are:

public delegate void TimerCompleteDelegate();

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

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CancelWaitableTimer(IntPtr hTimer);

You can use those functions in the following way:

public static IntPtr SetWakeAt(DateTime dt)
{
    TimerCompleteDelegate timerComplete = null;

    // read the manual for SetWaitableTimer to understand how this number is interpreted.
    long interval = dt.ToFileTimeUtc(); 
    IntPtr handle = CreateWaitableTimer(IntPtr.Zero, true, "WaitableTimer");
    SetWaitableTimer(handle, ref interval, 0, timerComplete, IntPtr.Zero, true);
    return handle;
}

You can then cancel the waitable timer with CancelWaitableTimer, using the returned handle as an argument.

Your program can hibernate and sleep using pInvoke:

[DllImport("powrprof.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent);

public static bool Hibernate()
{
    return SetSuspendState(true, false, false);
}

public static bool Sleep()
{
    return SetSuspendState(false, false, false);
}

Your system may not allow programs to let the computer enter hibernation. You can call the following method to allow hibernation:

public static bool EnableHibernate()
{
    Process p = new Process();
    p.StartInfo.FileName = "powercfg.exe";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo.Arguments = "/hibernate on"; // this might be different in other locales
    return p.Start();
}
狠疯拽 2024-08-03 17:10:19

Win7中的任务调度程序taskschd.msc(我相信XP也是如此)可以设置为在不同的触发器上唤醒系统。 这些触发器可以是计划、时间、事件等。

至少在 Win7 中,您需要将“允许唤醒计时器”设置为“启用”才能使其工作。 此设置位于...

--> 控制面板\硬件和声音\电源选项
单击 - “编辑计划设置”
单击 - “更改高级电源设置”
展开 - “睡眠”
展开 - “允许唤醒计时器”

The task scheduler program in Win7, taskschd.msc (and I beleive XP as well) can be set to wake the system on different triggers. Those triggers can be schedule, time, event, etc.

In at least Win7, you need to set "Allow Wake Timers" to 'Enabled' for this to work. This setting is found under...

--> Control Panel\Hardware and Sound\Power Options
click - "Edit Plan Settings"
click - "Change advanced power setting"
expand - "Sleep"
Expand - "Allow Wake timers"

心凉怎暖 2024-08-03 17:10:19

最好的选择是使用LAN唤醒功能。 这将需要另一台机器发送特殊类型的数据包来唤醒您的机器。

如果您的计算机未连接到网络,或者由于某种原因您不想将此逻辑移动到另一台计算机上,这将没有帮助。 但对于拥有多台计算机并希望以编程方式唤醒它们的某些配置来说,它非常有用。

Your best bet is using Wake on LAN capability. This will require another machine to send a packet of a special kind to wake your machine up.

This will not be helpful if your machine is not connected to the network or you for some reason don't wasnt to move this logic onto another machine. But it's useful for some configurations where you have multiple machines and want to wake them up programmatically.

江湖彼岸 2024-08-03 17:10:19

有些机器有 BIOS 闹钟,可以设置在特定时间唤醒计算机。 应该可以对这个时钟进行编程,但我不知道具体细节。

编辑:我发现这个程序应该让你设置时间。 它是在 Linux 下用 C 语言编写的,但也许它可以给你一些提示。

不过警告:在尝试直接更改 BIOS 设置之前,请务必记下 BIOS 屏幕上的每个设置,因为如果出现错误,BIOS 可能会恢复为出厂状态默认,您可能需要按原样再次设置。

Some machines have a BIOS alarm clock that can be set to wake up the computer at a certain hour. It should be possible to program this clock, but I don't know the specific details.

Edit: I found this program that should let you set the time. It's in C, under Linux, but maybe it can give you some hints.

A warning though: before trying anything that changes the BIOS settings directly be sure to write down every setting from BIOS screens, because in case of an error the BIOS might revert to factory default and you might need to set it up again as it was.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文