locked
DatagramSocket; GetOutputStreamAsync asynch?

    Question

  • The function GetOutputStreamAsync returns IAsyncOperation<IOutputStream> right away, rather than through a callback. See http://msdn.microsoft.com/en-us/library/windows/apps/hh701265

    So how is this asynch, and how do I know when it completed? Or can I use the IOutputStream right away and data I write is buffered internally?

    Tuesday, January 1, 2013 7:12 PM

Answers

  • Here is the sample which may help you

    HRESULT  taskResult = S_OK;
    IOutputStream^ streamWriter;
    
    create_task( datagramSocket->GetOutputStreamAsync(destinationAddress, destinationPort) ).then([&] (IOutputStream^ stream)
    	{
    		try
    		{
    			streamWriter = stream;
    		}
    		catch (Exception^ exception)
    		{
    			taskResult = exception->HResult;
    		}
    	}).get();
    
    	if (taskResult == S_OK)
    	{
    		return streamWriter;
    	}
            else
    	        return nullptr;

    Another way,

    task<void>( UdpSocket->ConnectAsync( ref new HostName(remoteHostName), remoteServiceName ) ).then([=] (task<void> previousTask)
        {
            try
            {
                previousTask.get();
                OutputDebugStringA("........Connected \n");
    	    connectionStatus = ConnectionStates::CONNECTED;
            }
            catch (Exception^ exception)
    
            {
    			OutputDebugStringA(".........Connect failed with error: \n");
    			connectionStatus = ConnectionStates::CONNECTFAILED;
            }
        });

    • Marked as answer by Jesse Jiang Thursday, January 3, 2013 3:13 AM
    Wednesday, January 2, 2013 3:06 AM

All replies

  • Here is the sample which may help you

    HRESULT  taskResult = S_OK;
    IOutputStream^ streamWriter;
    
    create_task( datagramSocket->GetOutputStreamAsync(destinationAddress, destinationPort) ).then([&] (IOutputStream^ stream)
    	{
    		try
    		{
    			streamWriter = stream;
    		}
    		catch (Exception^ exception)
    		{
    			taskResult = exception->HResult;
    		}
    	}).get();
    
    	if (taskResult == S_OK)
    	{
    		return streamWriter;
    	}
            else
    	        return nullptr;

    Another way,

    task<void>( UdpSocket->ConnectAsync( ref new HostName(remoteHostName), remoteServiceName ) ).then([=] (task<void> previousTask)
        {
            try
            {
                previousTask.get();
                OutputDebugStringA("........Connected \n");
    	    connectionStatus = ConnectionStates::CONNECTED;
            }
            catch (Exception^ exception)
    
            {
    			OutputDebugStringA(".........Connect failed with error: \n");
    			connectionStatus = ConnectionStates::CONNECTFAILED;
            }
        });

    • Marked as answer by Jesse Jiang Thursday, January 3, 2013 3:13 AM
    Wednesday, January 2, 2013 3:06 AM
  • OK Thanks.
    Wednesday, January 2, 2013 4:15 PM