Sajid Wasim
Sajid Wasim

Reputation: 71

ASP.NET ASYNC WAIT TASK not processing for multiple request

When I am using an async task, after finishing the task process (** marked) it gets stuck and doesn't go to the second line (populating the datagridview). If I remove the async task process, it works fine. If I use this async task for a single request XML it works fine, but multiple get stuck.

In this process I am using an Excel file that contains n number of data, mapping with a request XML body and post for response.

protected async void btnProcessFile_Click(object sender, EventArgs e)
{
    var fubsServiceData = GetFubsServiceData.GetInstance();
    string serviceCategory = ddlAcctServiceCatg.SelectedValue;

    if (!fuFileUpload.HasFile) return;
    if (serviceCategory == "0")
    {
        MessageBox.Show("Please select a category.");
    }
    else
    {
        try
        {
            var fileName = GetFileName();
            switch (serviceCategory)
            {
                case "CIF":
                    **gvData.DataSource = await Task.Run(() =>
                        fubsServiceData.GetFubsServiceCifResponseInfo(fileName));**
                    gvData.DataBind();
                    DeleteFile(fileName);
                    btnExportToExcel.Visible = true;
                    break;

                case "CLE":
                    gvData.DataSource = await Task.Run(() =>
                        fubsServiceData.GetFubsServiceClResponseInfo(fileName));
                    gvData.DataBind();
                    DeleteFile(fileName);
                    btnExportToExcel.Visible = true;
                    break;
            }

            foreach (GridViewRow row in gvData.Rows)
            {
                string rowType = row.Cells[0].Text;

                row.ForeColor = rowType == "FAILURE" ? Color.DarkRed : Color.Green;
            }
            if (gvData.Rows.Count != 0)
            {
                MessageBox.Show(@"Process completed successfully.");
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }
}

Upvotes: 2

Views: 710

Answers (1)

John
John

Reputation: 728

It looks like you are using webForm as your solution. It too old and does not support async I think (correct me if I am wrong). Unlike client-side WPF and Windows Forms, an ASP.NET pipeline is sync only and has a SynchronizationContext to ensure that.

Of course, this (SynchronizationContext) is not your issue here, because you use Task.Run. It is because an ASP.NET webform has only one thread for processing one client request. After all the work that the ASP.NET pipeline finish process(sync return), it then hooks into the IIS pipeline to finish other things (like the HTTP header, rewrite-outbound-rewrite).

So, here if you are using async void, then the pipeline will end immediately, and because IIS is sending all things to the client, it will kill the thread, so after the await , the thread does not even exist.

You can use Task.Run to make things parallel, but it not async. And it will use more threadPool threads.

To use Parallel, you also should use yourTask.GetAwater().GetResult() to wait for things to finish.

webFom can support async as @paulo-morgado said.

So to use async, you need:

  1. <%@ Page Async="true" Language="C#" AutoEventWireup="true" ... %>
  2. Use async Task instead of async void, so webForm can know when the method is finished

Upvotes: 1

Related Questions