WinRT: DataReader.LoadAsync Exception with StreamSocket TCP

mentras picture mentras · Apr 19, 2013 · Viewed 6.9k times · Source

I'm programming a client app on WinRT in C# which connects to several servers by TCP. For the TCP connection I use StreamSocket. The Input and Output Strings are then wrapped in a DataWriter and a DataReader. When I connect to more than one server I get the following exception: "Operation identifier is not valid"

This is the code of the method:

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);

        }
    }

The Stacktrace only shows the reader.LoadAsync(UInt32 count) method as the root of the problem. Each ClientInstance is running in an own task and has it's own DataReader and Stream instance. The "receiveBufferSize" is at 8192 Bytes.

Do you have any idea what the error could be?

Answer

mentras picture mentras · Apr 20, 2013

I think I can answer my question by myself now. The issue was that the LoadAsync method doesn't work so well together with the await/async construct. The method was called by ThreadPool Thread A and then resumed (after await) by ThreadPool Thread B. This constellation threw the Exception. But I can't exactly say why...

With this answer (How to integrate WinRT asynchronous tasks into existing synchronous libraries?) I wrote the LoadAsync method into a synchronous method and now it works because the same thread is calling the method and uses the results of it.

Here is the modified code fragment:

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

Thanks Geoff for bringing me on to the right path with the threads :) I hope I can help someone who also (will) have this issue.