WinRT的:DataReader.LoadAsync异常与StreamSocket TCP [英] WinRT: DataReader.LoadAsync Exception with StreamSocket TCP

查看:908
本文介绍了WinRT的:DataReader.LoadAsync异常与StreamSocket TCP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程的WinRT的在C#它通过TCP连接到多台服务器一个客户端应用程序。对于TCP连接我用StreamSocket。输入和输出字符串,然后包裹在DataWriter和一个DataReader。当我连接到多台服务器,我得到以下异常:
操作标识符无效

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"

这是该方法的代码:

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

        }
    }



堆栈跟踪只显示阅读器。 LoadAsync(UInt32的计数)方法,作为问题的根源。
分别ClientInstance是在自己的任务运行,并且有它自己的DataReader和流实例。在receiveBufferSize是8192字节。

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?

推荐答案

我想我现在可以由自己回答我的问题。问题是,LoadAsync方法不具有的await /异步结构,以便很好地协同工作。该方法是由线程池线程A调用,然后重新开始(等待之后)由线程池线程B.这个星座抛出异常。但我无法确切说出为什么...

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...

通过这个答案(的如何的WinRT异步任务融入?)我写的LoadAsync方法为同步方法现有的同步库和现在的作品,因为相同的。线程调用该方法,并使用它的结果。

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.

这篇关于WinRT的:DataReader.LoadAsync异常与StreamSocket TCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆