Writing asynchronous problems to the server

The application is designed for the Windows 8 Mobile platform, and it tends to crash when I send some data to the server at some point in the life cycle.

            await writer.StoreAsync();

An exception of type "System.Exception" occurred in mscorlib.ni.dll, but was not processed in user code

Additional Information: The established connection was interrupted by software on your host machine. (Exception from HRESULT: 0x80072745)

Even if I put it in a try catch, I cannot throw an exception, since it feels like it is still closing the program.

When a button is clicked, it sends a message to the server;

       private  void button_Click(object sender, RoutedEventArgs e)
    {

        Task t = new Task(TalkToServer);

        if (t.IsCompleted != t.IsCompleted)
        {
        }
        else
        {
            t.RunSynchronously();


        }
    }

Therefore, if I press the button, it will start a new task. If the task is completed, run the task, otherwise, if it is not.

    private async void TalkToServer()
    {
        if (clientSocket == null)
            return;
        DataWriter writer = new DataWriter(clientSocket.OutputStream);

        MessageData md = new MessageData();
        md.MyUser = new User("us", "ps");
        md.MyType = MessageData.mType.REQUESTMEMBERS;
        string output = JsonConvert.SerializeObject(md);
        writer.WriteString(output);
        await writer.StoreAsync();


        await writer.FlushAsync();
        writer.DetachStream();
    }

, , await writer.StoreAsync(); . , ,

---- , , :

        private async void button_Click(object sender, RoutedEventArgs e)
    {
        await sendInfo();
    }

    private async Task sendInfo()
    {

        if (clientSocket == null)
            return;
        DataWriter writer = new DataWriter(clientSocket.OutputStream);

        MessageData md = new MessageData();
        md.MyUser = new User("us", "ps");
        md.MyType = MessageData.mType.REQUESTMEMBERS;
        string output = JsonConvert.SerializeObject(md);
        writer.WriteString(output);
        await writer.StoreAsync();


        await writer.FlushAsync();
        writer.DetachStream();
        return ;
    }

+4

Source: https://habr.com/ru/post/1629260/


All Articles