locked
How to transform an asynchronous call to a synchronous

    Question

  • Hi,

    I would like to port a sdk using traditional win32 api and synchronous calls into a compliant winrt sdk and I would like to know how I can transform  a asynchronous call into a synchronous one. For instance I would like to implement CreateFile like this (code not functional, not tested, ...) :

    typedef struct _HANDLE_PRIVATE
    {
       ComPtr<IStream> sourceStream;
    } HANDLE_PRIVATE;
    
    HANDLE
    WINAPI
    CreateFileW(
        _In_ LPCWSTR lpFileName,
        _In_ DWORD dwDesiredAccess,
        _In_ DWORD dwShareMode,
        _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
        _In_ DWORD dwCreationDisposition,
        _In_ DWORD dwFlagsAndAttributes,
        _In_opt_ HANDLE hTemplateFile
        )
    {
        HANDLE_PRIVATE hFile;
    
        auto fileOp = StorageFile::GetFileFromPathAsync(lpFileName);
        fileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile ^>([=]IAsyncOperation<StorageFile^>^ a)
        {
           ///
        });
    
    fileOp->Start();
    
    
        return (HANDLE)hFile;
    }

    But in c++/cx how can I do a blocking wait ?

    Friday, June 15, 2012 7:53 AM

Answers

  • Are you calling this on a UI thread or on a worker thread?

    If you are calling it on a worker thread then you can call task::wait to wait for the task to finish.

    If you are on the UI thread then making the task synchronous may hang the UI: your users will be much happier if you don't do this. Instead of blocking waiting for the task to finish put the tasks which depend on the login in your completion function.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 18, 2012 11:31 AM

All replies

  • Only way is to use that in thread where you can use wait.
    Friday, June 15, 2012 10:19 AM
  • Are you calling this on a UI thread or on a worker thread?

    If you are calling it on a worker thread then you can call task::wait to wait for the task to finish.

    If you are on the UI thread then making the task synchronous may hang the UI: your users will be much happier if you don't do this. Instead of blocking waiting for the task to finish put the tasks which depend on the login in your completion function.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 18, 2012 11:31 AM