1

私のアプリケーションはユーザーの資格情報をサーバーに送信します。正しい場合、サーバーはクライアントに資格情報が間違っているか正しいかを伝えるパケットを送信します。いくつかのダミー テストの後、このコード行が原因でクライアントがクラッシュしました。

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

タイプ 'System.ObjectDisposedException' の例外が MobileApp.exe で発生しましたが、ユーザー コードで処理されませんでした

Object Disposable Exception は、ユーザー コードによって処理されませんでした。

4

2 に答える 2

1

非同期メソッド内から LoadAsync を呼び出すと、厄介な問題が発生します。LoadAsync メソッドは、UI スレッドから 1 回だけ呼び出す必要があります。LoadCompleted イベントが発生するまで、このメソッドを再度呼び出すことはできません。ほとんどの場合、前のリクエストが完了する前にもう一度呼び出します。

問題のある await 行を次のように切り替えてみてください。

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

より詳細な非常に長い回答が必要な場合は、次の回答を参照してください: https://stackoverflow.com/a/9898995/3299157

于 2016-02-17T14:38:28.327 に答える