locked
How can I transform an asynchronous call into a synchronous one in c++

    Question

  • Hi,

    I am porting a native sdk to WinRT and I would like to know how I can block the async. methods GetDisplayNameASync ?
    I know that Windows 8 doesn't like synchronous call  but I have no choice because I don't want to rewrite the sdk entirely
    so the asynchronous operation will be made on the caller side.

    I was not able to find one example showing in C++ how to wait for the completion

     

    BOOL
    WINAPI
    GetUserNameW (
        LPWSTR lpBuffer,
        LPDWORD pcbBuffer
        )
    {
    
    	IAsyncOperation<String^>^ asyncOp = GetDisplayNameAsync()
    	// HOW WAIT	
    
    	return TRUE;
    }

    Thanks

    Wednesday, June 20, 2012 3:31 PM

Answers

  • As suggested in your other thread on this, you can call task::wait to wait for the task to complete or be cancelled.

    This can only be done on a worker thread, not on the UI thread. The UI thread must remain responsive and so synchronous functions which may take significant time are forbidden.

    I strongly suggest you familiarize yourself with Asynchronous programming in C++ and attempt to work with the system rather than fighting against it.

    --Rob

    • Marked as answer by Jesse Jiang Friday, June 29, 2012 7:40 AM
    Thursday, June 21, 2012 12:05 AM
    Owner
  • task<String^>(UserInformation::GetDisplayNameAsync()).then([&](Platform::String^ username) {
    	wcsncpy(lpBuffer, (LPCWSTR)username->Data(), *pcbBuffer);
    }).wait();
    And wait and get you cannot use from UI thread (this is uncaught exception you get) !
    Friday, June 22, 2012 12:54 PM

All replies

  • As suggested in your other thread on this, you can call task::wait to wait for the task to complete or be cancelled.

    This can only be done on a worker thread, not on the UI thread. The UI thread must remain responsive and so synchronous functions which may take significant time are forbidden.

    I strongly suggest you familiarize yourself with Asynchronous programming in C++ and attempt to work with the system rather than fighting against it.

    --Rob

    • Marked as answer by Jesse Jiang Friday, June 29, 2012 7:40 AM
    Thursday, June 21, 2012 12:05 AM
    Owner
  • I am trying this but cannot find syntax in c++ to make it compile :

    BOOL
    WINAPI
    GetUserNameW (
        _Out_writes_to_opt_(*pcbBuffer, *pcbBuffer) LPWSTR lpBuffer,
        _Inout_ LPDWORD pcbBuffer
        )
    {
    	try {
    		create_task(UserInformation::GetDisplayNameAsync()).then([&](Platform::String^ username) {
    		
    		wcsncpy(lpBuffer, (LPCWSTR)username->Data(), *pcbBuffer);
    
    	}).wait();
    	}
    	catch(...)
    	{
    
    	}
    
    	return TRUE;
    }

    1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\concrt.h(4875): error C3861: '__uncaught_exception': identifier not found

    Don't understand what is the problem ?
    Actually it happens as soon as I include <ppltasks.h>

    Thanks




    Thursday, June 21, 2012 9:43 AM
  • task<String^>(UserInformation::GetDisplayNameAsync()).then([&](Platform::String^ username) {
    	wcsncpy(lpBuffer, (LPCWSTR)username->Data(), *pcbBuffer);
    }).wait();
    And wait and get you cannot use from UI thread (this is uncaught exception you get) !
    Friday, June 22, 2012 12:54 PM