locked
StreamSocket get an error “A method was called at an unexpected time.”

    Question

  • When running "writer->StoreAsync()" , popup the error "A method was called at an unexpected time."

    My code as the follow:

    StreamSocket^ socket = ref new StreamSocket();

    create_task(socket->ConnectAsync(hostName, hostport,
     isSsl ? SocketProtectionLevel::Tls10 : SocketProtectionLevel::PlainSocket
     )).then([this](task<void> previousTask)
    {
       try{
      previousTask.get();

     NotifyUser("Connected", NotifyType::StatusMessage);
     }
     catch (Exception^ exception)
     {
     NotifyUser("Connect failed with error: " + exception->Message, NotifyType::ErrorMessage);
     }
    });

    DataWriter^ writer = ref new DataWriter(socket->OutputStream);

    writer->UnicodeEncoding = UnicodeEncoding::Utf8;

    writer->ByteOrder = ByteOrder::LittleEndian;

    writer->WriteString(sendstr + "\r\n");

    create_task(writer->StoreAsync()).then([this, sendstr](task<unsigned int> writeTask)
    { try
     {
     writeTask.get();
     NotifyUser("\"" + sendstr + "\" sent successfully", NotifyType::StatusMessage);
     }
     catch (Exception^ exception)
     {
     NotifyUser("Send failed with error: " + exception->Message, NotifyType::ErrorMessage);
     }

    });

    Wednesday, May 14, 2014 11:30 AM

Answers

  • Hi,

    You should call the StoreAsync method after the StreamSocket connected complement. Try to edit your code like below:

    Windows::Networking::Sockets::StreamSocket^ streamSocket = ref new Windows::Networking::Sockets::StreamSocket();
    Windows::Networking::HostName^ hostName = ref new Windows::Networking::HostName(ref new Platform::String(L"www.microsoft.com") );  // any site
    Concurrency::task<void> connectTask(streamSocket->ConnectAsync(hostName, ref new Platform::String(L"80")));
    connectTask.then([this](task<void> prevTask){
    try{
        prevTask.get();
        
    }
    catch (Exception^ exception){
        
    }
    }).then([this,streamSocket](){
    	DataWriter^ writer = ref new DataWriter(streamSocket->OutputStream);
    
    writer->UnicodeEncoding = UnicodeEncoding::Utf8;
    
    writer->ByteOrder = ByteOrder::LittleEndian;
    
    writer->WriteString(sendstr + "\r\n");
    
    create_task(writer->StoreAsync()).then([this, sendstr](task<unsigned int> writeTask)
     { try
      {
      writeTask.get();
     
      }
      catch (Exception^ exception)
      {
     
      }
    
    });
    
    });

    And you should refer to the link to get more information about Asynchronous programming in C++:

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

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 15, 2014 12:01 PM

All replies

  • When running "writer->StoreAsync()" , popup the error "A method was called at an unexpected time."

    My code as the follow:

    StreamSocket^ socket = ref new StreamSocket();

    create_task(socket->ConnectAsync(hostName, hostport,
     isSsl ? SocketProtectionLevel::Tls10 : SocketProtectionLevel::PlainSocket
     )).then([this](task<void> previousTask)
    {
         try{
         previousTask.get();

         NotifyUser("Connected", NotifyType::StatusMessage);
     }
     catch (Exception^ exception)
     {
         NotifyUser("Connect failed with error: " + exception->Message, NotifyType::ErrorMessage);
     }
    });

    DataWriter^ writer = ref new DataWriter(socket->OutputStream);

    writer->UnicodeEncoding = UnicodeEncoding::Utf8;

    writer->ByteOrder = ByteOrder::LittleEndian;

    writer->WriteString(sendstr + "\r\n");

    create_task(writer->StoreAsync()).then([this, sendstr](task<unsigned int> writeTask)
    { try
     {
     writeTask.get();
     NotifyUser("\"" + sendstr + "\" sent successfully", NotifyType::StatusMessage);
     }
     catch (Exception^ exception)
     {
     NotifyUser("Send failed with error: " + exception->Message, NotifyType::ErrorMessage);
     }

    });

    Wednesday, May 14, 2014 11:33 AM
  • Hi,

    You should call the StoreAsync method after the StreamSocket connected complement. Try to edit your code like below:

    Windows::Networking::Sockets::StreamSocket^ streamSocket = ref new Windows::Networking::Sockets::StreamSocket();
    Windows::Networking::HostName^ hostName = ref new Windows::Networking::HostName(ref new Platform::String(L"www.microsoft.com") );  // any site
    Concurrency::task<void> connectTask(streamSocket->ConnectAsync(hostName, ref new Platform::String(L"80")));
    connectTask.then([this](task<void> prevTask){
    try{
        prevTask.get();
        
    }
    catch (Exception^ exception){
        
    }
    }).then([this,streamSocket](){
    	DataWriter^ writer = ref new DataWriter(streamSocket->OutputStream);
    
    writer->UnicodeEncoding = UnicodeEncoding::Utf8;
    
    writer->ByteOrder = ByteOrder::LittleEndian;
    
    writer->WriteString(sendstr + "\r\n");
    
    create_task(writer->StoreAsync()).then([this, sendstr](task<unsigned int> writeTask)
     { try
      {
      writeTask.get();
     
      }
      catch (Exception^ exception)
      {
     
      }
    
    });
    
    });

    And you should refer to the link to get more information about Asynchronous programming in C++:

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

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 15, 2014 12:01 PM