locked
can't initialize an IAsyncOperation inside create_async?

    Question

  • hi all,

    anyone knows this issue?

    Tuesday, May 08, 2012 8:34 AM

Answers

  • you can use task to do this async operation

    auto task1 = make_task([=] {
    	try
    	{
    		//async operation		
    	} 
    	catch (Exception^ e)
    	{
    		
    	}
    	});
    	structured_task_group tasks;
    	tasks.run_and_wait(task1);

    • Marked as answer by adewang82 Tuesday, May 15, 2012 3:07 AM
    Monday, May 14, 2012 9:53 AM

All replies

  • Hi adewang82

    you can create an IAsyncOperation handle to point you create_async,

    and before you can set status like this

    this is IAsyncAction sample

    you could reference it

    AsyncStatus connect_async = AsyncStatus::Error;

    StreamSocket^ socket; = refnewStreamSocket(); IAsyncAction^ _connect = socket->ConnectAsync(ref new HostName(IP), IP_port,SocketProtectionLevel::PlainSocket); connect_async = _connect->Status; while(connect_async != AsyncStatus::Completed) { WaitForSingleObjectEx(event_wait,2,false); connect_async = _connect->Status;

     // here you should set wait time out to protect it can't return and blocked

    }


     

     

     

     

     

     
    • Edited by oishixixi Wednesday, May 09, 2012 6:00 AM
    Wednesday, May 09, 2012 3:23 AM
  •  

    Hello,

     

    Would you please provide us some codes to describe what function do you want to achieve.

     

    You can return a task<> from create_async. Please check this document

    http://msdn.microsoft.com/en-us/library/windows/apps/hh750082.aspx

     

    Best regards,

    Jesse


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

    Wednesday, May 09, 2012 6:56 AM
  • IAsycOperation<IVector<int>^>^  MyClass:: TestFunc(IStorageFile^ file)

    {

    m_file = file;

    return create_async([this] -> IVector<int>^{

       IAsyncOperation<IStorageFile^>^ op = m_file->CopyAsync(....)..   //// this line will thow an exception when running

    });

    }

    Wednesday, May 09, 2012 11:32 AM
  • you can use task to do this async operation

    auto task1 = make_task([=] {
    	try
    	{
    		//async operation		
    	} 
    	catch (Exception^ e)
    	{
    		
    	}
    	});
    	structured_task_group tasks;
    	tasks.run_and_wait(task1);

    • Marked as answer by adewang82 Tuesday, May 15, 2012 3:07 AM
    Monday, May 14, 2012 9:53 AM