none
StreamSocket, DataWriter.StoreAsync(), DataReader.LoadAsync() -- Asynchronous problems

    Question

  • I'm creating a Win 8 store app in which I connect to a server, written in Java, using StreamSocket. When I run the app in debug, with breakpoints on StreamSocket.ConnectAsync(...), DataWriter.StoreAsync(), and DataReader.LoadAsync(...), it connects, sends the message, and receives a message back. However, once I remove any one of my breakpoints, that method doesn't do it's job. How can I can fix this issue? Here is my code:

    	public async void Connect()
            {
                try
                {
                    await socket.ConnectAsync(new Windows.Networking.HostName(ip),
                        "50000", SocketProtectionLevel.PlainSocket);
                    Connected = true;
                }
                catch (Exception e)
                {
                    if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown)
                    {
                        throw;
                    }
    
                    Windows.UI.Popups.MessageDialog md =
                        new Windows.UI.Popups.MessageDialog("Error: " + e.Message);
                    return;
                }
                return;
    
            }

    public async void HandShake() { try { //output writer = new DataWriter(socket.OutputStream); writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; byte[] nameBytes = Encoding.UTF8.GetBytes(Name.ToCharArray()); writer.WriteBytes(nameBytes); await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); writer.Dispose(); //input reader = new DataReader(socket.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; uint bytesAvailable = await reader.LoadAsync(4096); byte[] byArray = new byte[bytesAvailable]; reader.ReadBytes(byArray); string temp = Encoding.UTF8.GetString(byArray, 0, Convert.ToInt32(bytesAvailable)); temp = temp.Substring(0, temp.Length - 1); if (temp == "NAME OK") { GoodName = true; } reader.DetachStream(); reader.Dispose(); } catch (Exception e) { //await Task.WhenAll(tasks.ToArray()); if (SocketError.GetStatus(e.HResult) == SocketErrorStatus.Unknown) { throw; } Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("Error: " + e.Message); md.ShowAsync(); } }

    Thanks in advance!


    • Edited by czifro Sunday, January 26, 2014 1:09 AM grammar
    Sunday, January 26, 2014 1:05 AM

Answers

  • Most likely you call this methods one after another, and because they are both defined as async void, they do not cooperate with each other. You need:

    1) Define both methods as async Task, e.g. public async Task Connect() {...}

    2) Always call them using await, e.g.await Connect();


    Blog: http://lunarfrog.com/blog

    • Marked as answer by czifro Sunday, January 26, 2014 10:43 PM
    Sunday, January 26, 2014 4:10 PM

All replies

  • Most likely you call this methods one after another, and because they are both defined as async void, they do not cooperate with each other. You need:

    1) Define both methods as async Task, e.g. public async Task Connect() {...}

    2) Always call them using await, e.g.await Connect();


    Blog: http://lunarfrog.com/blog

    • Marked as answer by czifro Sunday, January 26, 2014 10:43 PM
    Sunday, January 26, 2014 4:10 PM
  • That did it. That fixed my issues. Thank you!
    Sunday, January 26, 2014 10:44 PM