读取方法在收到几个数据包后崩溃

Read method crashes after receiving a few packets

我的应用程序将用户凭据发送到服务器,如果它们是正确的,服务器将发送一个数据包告诉客户端他们的凭据是错误的还是正确的。经过几次虚拟测试后,我的客户端因这行代码而崩溃;

            var count = await reader.LoadAsync(512);

调试显示计数为0,

但在崩溃之前,我能够向服务器发送七 (7) 个虚拟请求。客户能够阅读七 (7) 个回复。

    private async void loginButtonEvent(object sender, RoutedEventArgs e)
    {
        //disable the button so the user can't spam the login button and flood packets 
        loginButton.IsEnabled = false;

        //if the user is connected to the server, then allow
        if(connected)
        {
            //get the stream
            DataWriter writer = new DataWriter(clientSocket.OutputStream);

            //custom class (like a packet IO, messafe frame/ framing
            MessageData md = new MessageData();
            md.MyUser = new User(usernameTextBox.Text, passwordTextBox.Text);
            md.MyType = MessageData.mType.LOGINREQUEST;
            //Serialize it into a string, thank you newton.Json
            string output = JsonConvert.SerializeObject(md);
            //write to the server
            writer.WriteString(output);
            //dedicate and push
            await writer.StoreAsync();
            //flush like you flush a toilet so it doesn't get clogged in the pipeline
            await writer.FlushAsync();
            //detatch the stream, not sure why?
            writer.DetachStream();

            //get the input stream
            DataReader reader = new DataReader(clientSocket.InputStream);
            //create string to hold the data
            string receivedData;
            //dynamically process the data
            reader.InputStreamOptions = InputStreamOptions.Partial;
            //store the bytes in count? why do we pass 512? What if I send a picture to the server and it is 1mb?
            var count = await reader.LoadAsync(512);
            //covert the byte to a string
            receivedData = reader.ReadString(count);
            //construct the md object into what the server sent
            md = JsonConvert.DeserializeObject<MessageData>(receivedData);
            switch (md.MyType)
            {
                case MessageData.mType.LOGINFAILED:
                    messageBox("Username or Password is wrong");
                    //Todo://
                    break;
                case MessageData.mType.LOGINSUCCESS:
                    messageBox("Logged");
                    //TODO: Go to login screen
                    break;
            }

            await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));


        }
        loginButton.IsEnabled = true;


    }

我再次对此进行了测试,这次我能够向服务器发送十二 (12) 个数据包,并接收到客户端能够解析的十二 (12) 个数据包。

再一次,错误在于

            var count = await reader.LoadAsync(512);

An exception of type 'System.ObjectDisposedException' occurred in MobileApp.exe but was not handled in user code

用户代码未处理对象一次性异常。

当您从异步方法中调用 LoadAsync 时,您将进入棘手的水域。 LoadAsync 方法只能从 UI 线程调用一次。在引发 LoadCompleted 事件之前,无法再次调用该方法。您很可能在上一个请求完成之前第二次调用它。

尝试将有问题的等待行切换为如下内容:

IAsyncOperation<int> asyncOp= reader.LoadAsync(512);
asyncOp.AsTask().Wait();
var count = asyncOp.GetResults();

如果您想要更详细的很长的答案,请参阅此答案:

             //start a new task!
            Task t = new Task(ListenForMessage);
            //if the currrent task is not completed, then return
            //this means it wont calll var count = await reader.LoadAsync(512); I.E the devil
            if (!t.IsCompleted)
                return;

            //else if the task is completed, then run it!
            t.RunSynchronously();

感谢 Ageonix,由于他的回答导致了这个实现,我能够得出这个结论。

信用到期时给予信用。