locked
Calling Async function from MFT in metro RRS feed

  • Question

  • I'm writing a MFT in metro that streams data back to the main app, but I ran into a problem when I try to write data to a IBuffer. It looks like the call to StoreAsync doesn't always complete before the next line is executed, how do I handle Async calls in MFT (DataWriter::StoreAsync())?

    code:

    void MyProject::DoSomething( UINT32 param )
    {
        EnterCriticalSection( &m_crit_sec );
    
        ComPtr< IRandomAccessStream > stream;
        HStringReference mem_ref( RuntimeClass_Windows_Storage_Streams_InMemoryRandomAccessStream );
    
        HRESULT hr = Windows::Foundation::ActivateInstance< ComPtr< IRandomAccessStream > >( mem_ref.Get(), &stream );
    
        if ( SUCCEEDED( hr ) )
        {
            ComPtr< IDataWriter > data_writer;
            ComPtr< IDataWriterFactory > data_writer_factory;
            ComPtr< IOutputStream > output_stream;
            ComPtr< IAsyncOperationWithProgress< UINT32, UINT32 > > op_progress;
            ComPtr< IAsyncOperation< UINT32 > > operation;
            ComPtr< IBuffer > buffer;
            HStringReference dw_ref( RuntimeClass_Windows_Storage_Streams_DataWriter );
            UINT64 buf_size = 0;
    
            hr = GetActivationFactory( dw_ref.Get( ), &data_writer_factory );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            hr = stream->GetOutputStreamAt( 0, &output_stream );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            hr = data_writer_factory->CreateDataWriter( output_stream.Get( ), &data_writer );
            if ( FAILED( hr ) )
            {
                goto done;
            }		
    
            data_writer->WriteUInt32( param );
            if ( FAILED( hr ) )
            {
                goto done;
            }		
    
            hr = data_writer->StoreAsync( operation.ReleaseAndGetAddressOf( ) );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            hr = data_writer->DetachBuffer( &buffer );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            hr = stream->get_Size( &buf_size );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            hr = buffer->put_Length( ( UINT32 )buf_size );
            if ( FAILED( hr ) )
            {
                goto done;
            }
    
            OutputDebugString( L"Sent successful\n" );
        }
    
    done:
        LeaveCriticalSection( &m_crit_sec );
    }
    thanks!
     
    Monday, August 20, 2012 8:31 PM

Answers

  • found the solution by implementing IAsyncOperationCompletedHandle< UINT32 >
    • Marked as answer by A. Sham Monday, August 20, 2012 11:00 PM
    Monday, August 20, 2012 11:00 PM