code muncher
code muncher

Reputation: 1682

Win 8 Metro mode: WaitForSingleObjectEx failed

I'm trying to implement something like Sleep(msec) behavior in metro mode like this

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);
} 

It prints: WaitForSingleObjectEx, failed ...Err: WAIT_FAILED: -->> 0 Windows documentation says GetLastError supports metro mode too, but System error code says only desktop mode ... that's weird! and 0 means Success but it didn't! Can anyone help me atleast with GetLastError .. I know this's simple implementation and I did something like this for Desktop mode too .. wondering what is wrong!

>>>>>>>>> EDIT <<<<<<<<

Not sure with GetLastError, but just achieve kind of Sleep, with

WaitForSingleObjectEx(GetCurrentThread(), ms, FALSE);

Upvotes: 3

Views: 932

Answers (1)

user63289
user63289

Reputation:

You may use C++ sleep_for which works in WinStore apps

#include <thread>
#include <chrono>

std::this_thread::sleep_for(std::chrono::milliseconds(1000));

Upvotes: 4

Related Questions