2

I'm new to ASP.net and I am facing a problem with asynchronous tasks.

Here is what I would like to do.

Let's say I have a web page named WebPage1 with a button Button1.

protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Text = "Working....";
        System.Threading.Thread.Sleep(20000);
        Button1.Text = "Finish !";
    }

I would like to be able to switch to WebPage2 without losing the task I started with Button 1. So when I go back to WebPage1 after 20seconds, I should be able to read "Finish !" on the button.

Is it possible? I stress that I have to do this with .net 4.0

I have alread been looking at

but I can't find a way to resolve my problem

Thanks in advance !

1

2 Answers 2

2

First You Will need to import.

using System.Threading.Tasks;

Then Your button handler should look something like this.

protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Text = "Working....";
        Task.Factory.StartNew(() =>
        {
               //Do Stuff
        });
    }

You will need to have some way to know when the task is finished, so that you can check it when you return on page load. This would most likely be a database value of some sort. It would allow you to tell the user if the task has finished or not.

Sign up to request clarification or add additional context in comments.

Comments

0

For asynchronous postback, you could try to use the updatepanel to implement this.

Below is a simple example which you could refer to:

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="Updt1" runat="server">
        <ContentTemplate>
            <asp:Label ID="lblError" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="btn" runat="server" Text="Click Update" onclick="btn_Click"/>
            <asp:UpdateProgress ID="UpdtProgress" DisplayAfter="1" AssociatedUpdatePanelID="Updt1" runat="server">
                <ProgressTemplate>
                    Please Wait
                </ProgressTemplate>
            </asp:UpdateProgress>
        </ContentTemplate>
    </asp:UpdatePanel>


protected void btn_Click(object sender, EventArgs e)
        {
            try
            {
                lblError.Visible = false;
                lblError.Text = "Start";
                System.Threading.Thread.Sleep(6000);
                lblError.Text = "End";
                lblError.Visible = true;
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
                lblError.Visible = true;
            }

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.