Webforms - update UI while waiting for long running task

时间:2018-02-05 12:52:18

标签: c# asp.net webforms async-await

I'm using c#/webforms for a form. The form also relies on UpdatePanels.

On the final step the event launches a long running (90 secs) HttpPost to a third party site.

At present when the submit button is pressed, I present a modal overlay while the process takes place, then redirect the user to another page on completion.

This is okay, but it doesn't refresh the update panel during the process, so the UI doesn't reflect the true picture - it's a step behind.

I've only just moved from Asp.Net 4.0 so not had the opportunity to investigate async await stuff until now.

I'm testing a new process but can't get it to work the way I want.

Here's the event that's called on the final step:

    protected async void Submit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Task<bool> post = PostApplication();

            var step = Int32.Parse(((LinkButton)sender).CommandArgument);

            SetStepState(step, StepState.Complete);
            upStepper.Update();

            var result = await PostApplication();

            Response.Redirect("/Result.aspx");
        }
    }


    public async Task<bool> PostApplication()
    {
        await Task.Delay(3000);
        return true;
    }

The task does its job properly and a result is returned in 3 seconds, followed by the redirect.

What is not happening is the UI isn't being updated during the wait. The method SetStepState() updates a data-attribute on the current step, which in turn should change how the step displays, but it doesn't. I've also forced an update to the update panel immediately after changes but nothing changes.

I'm not sure whether what I'm attempting is possible so would appreciate any advice.

0 个答案:

没有答案