UpdatePanel从调用不同线程的ajax刷新

问题描述 投票:1回答:1

我有一个按钮,可以进行ajax调用。

<%@ 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>

然后在我的后端,当单击提交按钮时,它将启动另一个耗时的过程。所以我将这个过程分成另一个任务。然后在任务完成后,我想更新结果。但它似乎没有用。

代码背后:

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;
}

因此,当该过程完成后,我想删除状态文本并启用后退按钮。但它仍然存在,按钮仍然被禁用。

怎么解决这个?

c# ajax multithreading
1个回答
1
投票

您可以使用SignalR将更改推送回客户端。它会自动选择最有效的协议(即Websockets,LongPolling等)

或者,当您启动任务时,在表中放置一个条目并将主键返回给客户端。客户端可以使用计时器继续轮询,以查看表中的状态是否已更改并执行适当的操作。

要检索状态,您可能必须在页面中公开新代码。

© www.soinside.com 2019 - 2024. All rights reserved.