简体   繁体   English

在通用Windows平台中使用SetWaitableTimer

[英]Using SetWaitableTimer in Universal Windows Platform

I use SetWaitableTimer from "kernel32.dll" in my UWP app. 我在UWP应用中使用“ kernel32.dll”中的SetWaitableTimer。 I want the computer to wake from sleep because of my application. 我希望计算机由于我的应用程序而从睡眠中唤醒。 If I run the app in debug mode it works. 如果我在调试模式下运行该应用程序,则可以正常运行。 If I run it in release mode then the computer doesn't wake up. 如果我以发布模式运行它,则计算机不会唤醒。

How can I do to make my application awakened the computer when it is running in realise mode? 当计算机以实现模式运行时,如何使我的应用程序唤醒计算机?

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Task.Factory.StartNew(SetWaitForWakeUpTime);
    }

    [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.dll", SetLastError = true)]
    public static extern Int32 WaitForSingleObject(IntPtr handle, uint milliseconds);

    static IntPtr handle;

    static void SetWaitForWakeUpTime()
    {
        long duetime = -600000000;

        handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
        SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
        uint INFINITE = 0xFFFFFFFF;
        int ret = WaitForSingleObject(handle, INFINITE);
    }

Windows 10 Universal Windows Platform (UWP) apps can use a subset of the Win32 and COM APIs. Windows 10通用Windows平台(UWP)应用程序可以使用Win32和COM API的子集。 This subset of APIs was chosen to support key scenarios for Windows Runtime apps that were not already covered by the Windows Runtime, HTML/CSS, or other supported languages or standards. 选择该API的子集来支持Windows Runtime应用程序,Windows Runtime,HTML / CSS或其他受支持的语言或标准尚未涵盖的关键方案。 The Windows App Certification Kit ensures that your app uses only this subset of the Win32 and COM API. Windows应用程序认证工具包可确保您的应用程序仅使用Win32和COM API的此子集。 For the Win32 APIs that can be used in UWP, please see Win32 and COM APIs for UWP apps . 有关可在UWP中使用的Win32 API,请参阅UWP应用程序的Win32和COM API

Unfortunately, CreateWaitableTimer and SetWaitableTimer are not in the supported APIs. 不幸的是,受支持的API中没有CreateWaitableTimerSetWaitableTimer We can not use them in UWP apps. 我们不能在UWP应用中使用它们。 For a UWP app, it should not be able to wake the system from sleep. 对于UWP应用,它应该无法将系统从睡眠中唤醒。 But we can prevent the system from sleep by using DisplayRequest class . 但是我们可以使用DisplayRequest类来防止系统进入睡眠状态。

Apps that show video or run for extended periods without user input can request that the display remain on by calling DisplayRequest::RequestActive . 无需用户输入即可显示视频或长时间运行的应用程序可以通过调用DisplayRequest :: RequestActive来请求保持显示状态。 When a display request is activated, the device's display remains on while the app is visible. 激活显示请求后,设备的显示将保持打开状态,同时显示该应用程序。

For more info, plesae see Remarks of DisplayRequest class . 有关更多信息,请参阅DisplayRequest类的 说明

But if you are developing apps for enterprise, you can try with Brokered Windows Runtime Components . 但是,如果您正在为企业开发应用程序,则可以尝试使用Brokered Windows Runtime Components With this, you have the ability to run existing desktop software assets in one process (desktop component) while interacting with this code in a UWP App. 这样,您就可以在一个进程(桌面组件)中运行现有的桌面软件资产,同时在UWP App中与此代码进行交互。 For more info, please check this blog . 有关更多信息,请检查此博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM