locked
Creating Async Operation with Multiple Tasks

    Question

  • hi,

    I'm creating a Metro style app and I need to use Async operations. I need some of my methods to happen after a certain Multiple task is done. I understood the only way of doing that is by making the whole thing asynchronous and using the key word then.([]..

    Is there any way of creating an IAsyncOperation<UINT>^ or an IAsyncAction^ with a Multiple task chain in it, and the next task will happen only after the multiple task is done?

    example;

    IAsyncOperation<UINT>^ CVideoDecAVC::openInputStream(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]() -> UINT
        { 
             auto OpenFileTask = create_task(fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
            OpenFileTask.then([this](IRandomAccessStream ^RandomAccessStream)
            {
                   //do something
                   auto create_task(inputStream->ReadAsync(buffer,bufSize,(InputStreamOptions)1));
            }).then([this]()
            {
                    //do something
            });
            return hr;
        });
    }
    
    IAsyncAction^ CContentProtectionD3D11::CreateDecoder(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]
        {
             //do something
             auto OpenStreamTask = create_task(openInputStream(fileHandle));
            OpenStreamTask.then([this](UINT hr)
            {           
               //do something
            });//task end
        });//async end
    }
    
    

    thanks
    Monday, December 24, 2012 11:33 AM

Answers

  • I found a solution!

    what I did was

    IAsyncOperation<UINT>^ CVideoDecAVC::openInputStream(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]() -> UINT
        { 
             auto OpenFileTask = create_task(fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
            OpenFileTask.then([this](IRandomAccessStream ^RandomAccessStream)
            {
                   //do something
                   auto create_task(inputStream->ReadAsync(buffer,bufSize,(InputStreamOptions)1));
            }).then([this]()
            {
                    //do something
            }).wait();
            return hr;
        });
    }
    
    IAsyncAction^ CContentProtectionD3D11::CreateDecoder(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]
        {
             //do something
             auto OpenStreamTask = create_task(openInputStream(fileHandle));
            OpenStreamTask.then([this](UINT hr)
            {           
               //do something
            }).wait();//task end
        });//async end
    }
    

    this way the whole multiple task will happen before returning.

    you can use the key word wait here because the AsynAction is a separate thread.

    • Marked as answer by spaarvap Thursday, December 27, 2012 11:35 AM
    Thursday, December 27, 2012 11:35 AM

All replies

  • When I need to do more than one asynchronous event I put subsequent function in the continuation of the previous asynchronous operation.


    n.Wright

    Tuesday, December 25, 2012 12:20 AM
  • That sounds like a good idea. But my problem is I'm working in two separate classes. In one class (a), I call a function from another class (b) which contains a multiple task. I want to wait for the whole chain to end before returning from the function in class (b) and continuing my work in class (a).
    Tuesday, December 25, 2012 7:49 AM
  • I found a solution!

    what I did was

    IAsyncOperation<UINT>^ CVideoDecAVC::openInputStream(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]() -> UINT
        { 
             auto OpenFileTask = create_task(fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read));
            OpenFileTask.then([this](IRandomAccessStream ^RandomAccessStream)
            {
                   //do something
                   auto create_task(inputStream->ReadAsync(buffer,bufSize,(InputStreamOptions)1));
            }).then([this]()
            {
                    //do something
            }).wait();
            return hr;
        });
    }
    
    IAsyncAction^ CContentProtectionD3D11::CreateDecoder(StorageFile^ fileHandle)
    {
        return create_async([this,fileHandle]
        {
             //do something
             auto OpenStreamTask = create_task(openInputStream(fileHandle));
            OpenStreamTask.then([this](UINT hr)
            {           
               //do something
            }).wait();//task end
        });//async end
    }
    

    this way the whole multiple task will happen before returning.

    you can use the key word wait here because the AsynAction is a separate thread.

    • Marked as answer by spaarvap Thursday, December 27, 2012 11:35 AM
    Thursday, December 27, 2012 11:35 AM