Answered AsyncActionCompletedHandler status

  • Wednesday, May 09, 2012 6:41 PM
     
      Has Code

    Hi,

    I add a  AsyncActionCompletedHandler to be notified when StreamScoket.ConnectAsync operation completes.

    MSD documentation for this handler is

    public delegate void AsyncActionCompletedHandler(
      IAsyncAction asyncInfo, 
      AsyncStatus asyncStatus
    )

    IAsyncAction also has an AsyncStatus member Status, is this the same as the AsyncStatus passed to the handler or they reflect different things?

    If the operation fails will asyncInfo->ErrorCode.Value contain the SocketErrorStatus?

All Replies

  • Thursday, May 10, 2012 8:47 AM
    Moderator
     
     Answered

    Hello,

     

    AsyncStatus only return the previous async operator status like Canceled, Completed, Error, Started.

     

    If your ConnectAsync failed with error. The asyncStatus return 3, and it is not the SocketErrorStatus.

     

    The SocketErrorStatus error you should use SocketError.GetStatus  to get is this is static function.

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.sockets.socketerror.getstatus.aspx

     

    Best regards,

    Jesse


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

  • Thursday, May 10, 2012 3:18 PM
     
      Has Code
    To be sure i understand it correctly, could i do something like this
    AsyncOperationWithProgressCompletedHandler<IBuffer^, uint32> readInfo = ...;
    
    readInfo->Completed = ref new AsyncOperationWithProgressCompletedHandler<IBuffer^, uint32>
        ([=](IAsyncOperationWithProgress<IBuffer^,uint32>^ info, AsyncStatus status)
                {
                    if(info->Status == AsyncStatus::Error)
                    {
                        int errorCode = SocketError::GetStatus();
                        // handle socket error.
                    }
                    else if(info->Status == AsyncStatus::Canceled)
                    {
                        // operation was canceled.
                    }
                    else
                    {
                        // operation success
                        IBuffer^ b = info->GetResults();
                    }
                });

  • Thursday, May 17, 2012 8:06 AM
    Moderator
     
     

    Hello,

    You should use asyncStatus==AsyncStatus::Error to get the error result.

    if(asyncStatus==AsyncStatus::Error)
                {
                    try
                    {
                        int res= info->GetResults();

                    }catch(Exception^ ex)
                    {
                     
                    }
                }

    You can also use try-catch block in the task.

    Best regards,
    Jesse


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