WinRT: DataReader.LoadAsync exception using StreamSocket TCP

I am programming a WinRT client application in C # that connects to multiple servers via TCP. For a TCP connection, I use StreamSocket. The input and output lines are then wrapped in a DataWriter and DataReader. When I connect to multiple servers, I get the following exception: "Operation ID is not valid"

This is the method code:

private async void read()
    {
        while (true)
        {
            uint bytesRead = 0;
            try
            {
                bytesRead = await reader.LoadAsync(receiveBufferSize);

                if (bytesRead == 0)
                {
                    OnClientDisconnected(this);
                    return;
                }
                byte[] data = new byte[bytesRead];
                reader.ReadBytes(data);
                if (reader.UnconsumedBufferLength > 0)
                {
                    throw new Exception();
                }

                OnDataRead(this, data);
            }
            catch (Exception ex)
            {
                if (Error != null)
                    Error(this, ex);
            }

            new System.Threading.ManualResetEvent(false).WaitOne(10);

        }
    }

Stacktrace only shows the reader.LoadAsync (UInt32 count) method as the root of the problem. Each ClientInstance works in its own task and has its own instance of DataReader and Stream. "ReceiveBufferSize" has a value of 8192 bytes.

Do you have any ideas on what might be a mistake?

+5
source share
2

, . , LoadAsync wait/async. ThreadPool Thread A, ( ) ThreadPool Thread B. . , ...

( WinRT ?) LoadAsync , , .

:

IAsyncOperation<uint> taskLoad = reader.LoadAsync(receiveBufferSize);
taskload.AsTask().Wait();
bytesRead = taskLoad.GetResults();

, :) , -, .

+8

, :

LoadAsync .

:

LoadAsync . , LoadCompleted . LoadCompleted , .

, async/await - :

[...] . , .

, , LoadAsync() ; .

+1

All Articles