Win 8 Metro模式:WaitForSingleObjectEx失败



我正试图在城域模式下实现类似睡眠(msec)行为的东西,比如这个

void win8_metro_mode_sleep(unsigned long long ms)
{
    HANDLE hEvent;
    DWORD ret;
    DWORD err;
    DWORD msec = ms;
    print_on_text_box("nMSEC: ");
    print_on_text_box(((DWORD)ms).ToString());
    hEvent =CreateEventEx(NULL,L"sleep_event",NULL,EVENT_MODIFY_STATE);
     /*OpenEvent(NULL, TRUE, L"sleep_event");
    if(hEvent == NULL){
        print_on_text_box(">>>OpenEvent failed<<<n");
    }*/
    /*if(ResetEvent(hEvent) == 0){
        print_on_text_box(">>>ResetEvent failed!<<<n");
    }*/
    ret = WaitForSingleObjectEx(hEvent, msec, true);
    if(ret == WAIT_TIMEOUT) {
        print_on_text_box("Sleep Success!n");
    }
    else if(ret == WAIT_OBJECT_0){
        print_on_text_box("WaitForSingleObjectEx, failed ...Err: The state of the specified object is signaled.n");
    }
    else if(ret == WAIT_IO_COMPLETION){
        print_on_text_box("WaitForSingleObjectEx, failed ...Err: WAIT_IO_COMPLETION.n");
    }
    else if(ret == WAIT_FAILED){
        print_on_text_box(" WaitForSingleObjectEx, failed ...Err: WAIT_FAILED: -->> ");
        err = GetLastError();
        //FormatMessage(
        //  FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        //  FORMAT_MESSAGE_FROM_SYSTEM |
        //  FORMAT_MESSAGE_IGNORE_INSERTS,
        //  NULL,
        //  err,
        //  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        //  (LPTSTR) &lpMsgBuf,
        //  0, NULL );
        textbox_test_result->Text = textbox_test_result->Text + err.ToString() +" <<< n";
    }
    CloseHandle(hEvent);
} 

它打印:WaitForSingleObjectEx,失败。。。错误:WAIT_FAILED:-->0Windows文档说GetLastError也支持城域模式,但系统错误代码说只有桌面模式。。。真奇怪!0表示Success,但不是!至少有人能帮我做GetLastError吗。。我知道这是一个简单的实现,我也为桌面模式做了类似的事情。。想知道怎么了!

>>>>>编辑<lt<lt<lt<lt

不确定使用GetLastError,但仅使用实现某种Sleep

WaitForSingleObjectEx(GetCurrentThread(), ms, FALSE);

您可以使用在WinStore应用程序中工作的C++sleep_for

#include <thread>
#include <chrono>
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

相关内容

  • 没有找到相关文章

最新更新