Server Busy message field when sending data for posting asynchronously

I created a Windows forms application that sends data (using a POST call) to a web service when a button is clicked. During production, sometimes users see the message "Server Busy" (as shown below).

error message image

I am using the Async Await template to send messages to the service. The following is a snippet of code:

private void button1_Click(object sender, EventArgs e)
{
   SendDataToHost();
}

private async void SendDataToHost()
{
   //Get the data which needs to be sent to the host..

   var result= await PostData(data);
    //If the service fails to process this data for any reason, then
    // it returns the result object with succeeded as false    
    if (result != null && !result.Succeeded)
    {
        MessageBox.Show("Failed");
    }
    else
    {
        MessageBox.Show("Success");
    }
}

public async Task<Result> PostData(Data data)
{
    Result result = await PostAsync("cases", data);

    return result;
}

I understand that the Server Busy field is populating because the UI thread is blocking. But I'm trying to understand that the purpose of using Async and Await is to keep the user interface responsive, but why is the user interface thread blocked?

, , , , ? , dev, , .

:

1) ConfigureWait(false). , , "POST" .

public async Task<Result> PostData(Data data)
{
    Result result = await PostAsync("cases",data).ConfigureAwait(false);
    return result;
}

2) Post .

private async void SendDataToHost()
{
   //Get the data which needs to be sent to the host..

   **var result= await Task.Run(()=>PostData(data));**

    //If the service fails to process this data for any reason, then
    // it returns the result object with succeeded as false    
    if (result != null && !result.Succeeded)
    {
        MessageBox.Show("Failed");
    }
    else
    {
        MessageBox.Show("Success");
    }   
}

, , , ?

+4
1

, . QuickBooks, , Google-, , , . , , .

Visual Studio - , . , . Q + A.

, , "", . , , "-", - , , . , . COM, . , COM-, . PostAsync() , , . , -, .

, COM, , . , . "" 60 , , , . - , .

- , - , . " ", , , . , OLE - , , , , .

- . , . , , , , . , , . , , . , , COM-, .

, , . , , , , . , IMessageFilter COM-, CoRegisterMessageFilter(). # MSDN.

, Register() , COM-, , , STA. , , Register() . , COM.

+3

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


All Articles