rcs
rcs

Reputation: 7197

UpdatePanel Refresh from ajax calling different thread

I have a button that will do an ajax call.

<%@ Page Language="C#" MasterPageFile="~/pj.master" AutoEventWireup="true" CodeFile="refreshData.aspx.cs" Inherits="Admin_refreshData" EnableEventValidation="false" %>

<%@ MasterType VirtualPath="~/pj.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>

    <script type="text/javascript">        
        function checkSubmit() 
        {
            if (confirm('Are you sure you want to refresh the data?')) {
                //Disable our button
                $('#<%: butSubmit.ClientID %>').attr("disabled", true);
                <%= ClientScript.GetPostBackEventReference(butSubmit, string.Empty) %>;

                $.ajax({
                    error: function (data) {
                        //The Ajax request was a failure.
                        alert('Fail to call batch job.');
                    },
                    success: function (data) {
                        $('#<%: lblMsg.ClientID %>').text('Batch job running. Please kindly wait.');
                    },
                    complete: function () {

                    }
                });
            }
            else
                return false;
        }       
    </script>

    <h1>Refresh Data</h1>
    <asp:Panel ID="PnlSearch" runat="server">
        <asp:UpdatePanel ID="updtPnlRefresh" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <table border="0" style="width: 100%">
            <tr>                
                <td>
                    Refresh Data: <asp:Button ID="butSubmit" runat="server" Text="Refresh" OnClientClick="return checkSubmit()" UseSubmitBehavior="false" OnClick ="butSubmit_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblMsg" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
            </ContentTemplate>
        </asp:UpdatePanel>


    </asp:Panel>        
</asp:Content>

Then on my backend, when the submit button is clicked, it will start another time-consuming process. So I am separating this process into another Task. Then after the Task finishes, I'd like to update the result. But it does not seem to work.

Code behind:

protected void Page_Load(object sender, EventArgs e)
{

}

protected void butSubmit_Click(object sender, EventArgs e)
{
    new System.Threading.Tasks.Task(RunBatchJob).Start();

    lblMsg.Text = "Batch job running. Please kindly wait.";
    butSubmit.Enabled = false;
}

private void RunBatchJob()
{
    string path = @"D:\Codes\RefreshData.exe";

    Master.RemoveErrorMessage();

    if (File.Exists(path))
    {
        Process process = new Process();
        process.StartInfo.FileName = path;
        process.Start();
        process.WaitForExit();
        int result = process.ExitCode;
        if (result == 0)
            Master.AddSuccessMessage("Batch job successfully run.");
        else
            Master.AddErrorMessage("Batch job failed to run properly.");
    }
    else
    {
        Master.AddErrorMessage("Batch job file not found.");
    }

    lblMsg.Text = "";
    butSubmit.Enabled = true;
}

So when the process has finished, I'd like to remove the status text and enable back the button. But it is still there and the button is still disabled.

How to solve this?

Upvotes: 1

Views: 43

Answers (1)

Ramesh
Ramesh

Reputation: 13266

You can either use SignalR for pushing changes back to client. It would automatically pick the most efficient protocol (i.e Websockets, LongPolling etc.)

Alternatively when you start the task place an entry in a table and return the primary key to the client. Client can keep polling using a timer to see if the status has changed in the table and make appropriate action.

To retrieve the status you may have to expose new code in the page.

Upvotes: 1

Related Questions