locked
[Metro/ c++/cx] streamsocket connect and send tasks

    Question

  • Hi!

    I create streamsocket and connectAsync 

    ..... CreateConnection()

    task<void>(socket->ConnectAsync(hostName, "22112", SocketProtectionLevel::PlainSocket)).then([] (task<void> previousTask) { try { previousTask.get(); CoreApplication::Properties->Insert("connected", nullptr); } catch (Exception^ exception) { } });



    then i, but how can i wait first task?

    ...... send()

    DataWriter^ writer; String^ stringToSend("Hello"); writer->WriteUInt32(writer->MeasureString(stringToSend)); writer->WriteString(stringToSend);task<unsigned int>(writer->StoreAsync()).then([this, socket, stringToSend] (task<unsigned int> writeTask) { try { if (!CoreApplication::Properties->HasKey("connected")) { //error } // Try getting an exception. writeTask.get(); } catch (Exception^ exception) { } });

    if (!CoreApplication::Properties->HasKey("connected"))

    how to complete the previous TASK, and then send the message? i need it in c++/cx for winrt

    CreateConnection();
    Send();





    Monday, March 25, 2013 8:30 AM

Answers

  • You may try by followings way but try to use WinRT's asynchronous style

    ->  Since APIs are asynchronous, you may use task chain where you can maintain the sequence of operation through IAsyncAction or IAsyncOperationand IAsyncOperationWithProgress or IAsyncActionWithProgress. If you are executing your code from UI thread you can use task chain for maintaining sequence, because UI thread can not allow any blocking for synchronization.

    task chains, -> myTask.then(…).then(…).then(…);

    Asynchronous programming in C++ (Windows Store apps)

    Asynchronous Programming in C++ Using PPL

    -> If you are executing your code from a separate worker thread you can use task like following for synchronization.

    //CreateConnection()

    HRESULT taskResult = S_OK; task<void>(socket->ConnectAsync(hostName, "22112", SocketProtectionLevel::PlainSocket)).then([] (task<void> previousTask) { try { previousTask.get(); } catch (Exception^ exception) { taskResult = exception->HResult; } }).get(); if (taskResult == S_OK) { // Success } else { // Failed }

    You also try by following way,

    volatile bool asyncOperationCompleted = false;
    
    	 task<void>( AsyncTask( ... ) ).then([&asyncOperationCompleted] (task<void> previousTask) { 
    		 try{
    			previousTask.get(); 
    			asyncOperationCompleted = true;
    		 } catch(Exception e) { 
    			 asyncOperationCompleted = true;
    		 });
    
    	while( false == asyncOperationCompleted ) 
    		CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);



    Monday, March 25, 2013 2:26 PM

All replies

  • You may try by followings way but try to use WinRT's asynchronous style

    ->  Since APIs are asynchronous, you may use task chain where you can maintain the sequence of operation through IAsyncAction or IAsyncOperationand IAsyncOperationWithProgress or IAsyncActionWithProgress. If you are executing your code from UI thread you can use task chain for maintaining sequence, because UI thread can not allow any blocking for synchronization.

    task chains, -> myTask.then(…).then(…).then(…);

    Asynchronous programming in C++ (Windows Store apps)

    Asynchronous Programming in C++ Using PPL

    -> If you are executing your code from a separate worker thread you can use task like following for synchronization.

    //CreateConnection()

    HRESULT taskResult = S_OK; task<void>(socket->ConnectAsync(hostName, "22112", SocketProtectionLevel::PlainSocket)).then([] (task<void> previousTask) { try { previousTask.get(); } catch (Exception^ exception) { taskResult = exception->HResult; } }).get(); if (taskResult == S_OK) { // Success } else { // Failed }

    You also try by following way,

    volatile bool asyncOperationCompleted = false;
    
    	 task<void>( AsyncTask( ... ) ).then([&asyncOperationCompleted] (task<void> previousTask) { 
    		 try{
    			previousTask.get(); 
    			asyncOperationCompleted = true;
    		 } catch(Exception e) { 
    			 asyncOperationCompleted = true;
    		 });
    
    	while( false == asyncOperationCompleted ) 
    		CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);



    Monday, March 25, 2013 2:26 PM
  • In addition you might find the following webcast on Channel 9 very useful (where .then is discussed).

    Async made simple with C++ PPL - code example starts at offset 4:37
    http://channel9.msdn.com/Events/Windows-Camp/Developing-Windows-8-Metro-style-apps-in-Cpp/Async-made-simple-with-Cpp-PPL


    MCAD.NET C# - http://www.Global-webnet.com/Blog

    Monday, March 25, 2013 4:09 PM
  • You may try by followings way but try to use WinRT's asynchronous styl

    if i add .get(); in task

    Unhandled exception at 0x51C773B3 (msvcr110d.dll) in App1.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

    volatile bool asyncOperationCompleted = false; task<void>( AsyncTask( ... ) ).then([&asyncOperationCompleted] (task<void> previousTask) { try{ previousTask.get(); asyncOperationCompleted = true; } catch(Exception e) { asyncOperationCompleted = true; }); while( false == asyncOperationCompleted ) CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);

    work!



    Tuesday, March 26, 2013 11:05 AM
  • Since you are trying to block asynchronous operation, check whether you are executing in a different worker thread. Because UI thread does not allow any blocking.

    Thread pool sample

    ThreadPool class


    Tuesday, March 26, 2013 11:47 AM
  • You may try by followings way but try to use WinRT's asynchronous style

    ->  Since APIs are asynchronous, you may use task chain where you can maintain the sequence of operation through IAsyncAction or IAsyncOperationand IAsyncOperationWithProgress or IAsyncActionWithProgress. If you are executing your code from UI thread you can use task chain for maintaining sequence, because UI thread can not allow any blocking for synchronization.

    task chains, -> myTask.then(…).then(…).then(…);

    Asynchronous programming in C++ (Windows Store apps)

    Asynchronous Programming in C++ Using PPL

    -> If you are executing your code from a separate worker thread you can use task like following for synchronization.

    //CreateConnection()

    HRESULT taskResult = S_OK; task<void>(socket->ConnectAsync(hostName, "22112", SocketProtectionLevel::PlainSocket)).then([] (task<void> previousTask) { try { previousTask.get(); } catch (Exception^ exception) { taskResult = exception->HResult; } }).get(); if (taskResult == S_OK) { // Success } else { // Failed }

    You also try by following way,

    volatile bool asyncOperationCompleted = false;
    
    	 task<void>( AsyncTask( ... ) ).then([&asyncOperationCompleted] (task<void> previousTask) { 
    		 try{
    			previousTask.get(); 
    			asyncOperationCompleted = true;
    		 } catch(Exception e) { 
    			 asyncOperationCompleted = true;
    		 });
    
    	while( false == asyncOperationCompleted ) 
    		CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);



    Hi on windows phone 8.

    CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);

    error and crash application.

    DXGI WARNING: Process is terminating. Using simple reporting. Please call ReportLiveObjects() at runtime for standard reporting. [ STATE_CREATION WARNING #0: ]
    DXGI WARNING: Live Producer at 0x00A84648, Refcount: 4. [ STATE_CREATION WARNING #0: ]
    DXGI WARNING: Live Object at 0x00A89BC0, Refcount: 2. [ STATE_CREATION WARNING #0: ]
    DXGI WARNING: Live                         Object :      1 [ STATE_CREATION WARNING #0: ]
    The program '[4808] TaskHost.exe' has exited with code -2146233078 (0x8013150a).

    how can i fix it.

    Tuesday, April 02, 2013 9:18 AM