ASP.NET异步标签更新

时间:2011-03-11 09:59:54

标签: c# asp.net asynchronous

我有一个运行时间很长的流程,我想在流程进行时更新页面上的标签,但我没有运气。

这是aspx:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Async.aspx.cs" Inherits="Website.structureDoc.Async" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="startAsyncButton" runat="server" Text="Run" onclick="startAsyncButton_Click"/>
        <asp:Button ID="cancelAsyncButton" runat="server" Text="Cancel" onclick="cancelAsyncButton_Click"/>

        <asp:label id="resultLabel" runat="server"></asp:label>

    </div>
    </form>
</body>
</html>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Website.structureDoc
{
    public partial class Async : System.Web.UI.Page
    {
        BackgroundWorker backgroundWorker1;
        protected void Page_Load(object sender, EventArgs e)
        {
            backgroundWorker1 = new BackgroundWorker();

            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        protected void startAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }
        }

        protected void cancelAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker1.CancelAsync();
            }
        }

        // This event handler is where the time-consuming work is done.
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; i <= 10; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    // Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(500);
                    worker.ReportProgress(i * 10);
                }
            }
        }

        // This event handler updates the progress.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
        }

        // This event handler deals with the results of the background operation.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                resultLabel.Text = "Canceled!";
            }
            else if (e.Error != null)
            {
                resultLabel.Text = "Error: " + e.Error.Message;
            }
            else
            {
                resultLabel.Text = "Done!";
            }
        }
    }
}

背景工人是不是正确的做法?

如果可能,我宁愿不使用AJAX。

2 个答案:

答案 0 :(得分:6)

这不起作用。问题是asp.net在请求/响应模型上工作。为了接收任何更新,客户端(浏览器)需要从服务器请求信息。为了更新您的标签,您需要向服务器发送一个新值请求,然后该值将显示该值,并以适当的值进行响应以供您显示。

最简单的方法是使用AJAX以某种方式对数据请求进行计时,或者设置计时器并让页面在设置计时器上刷新。最重要的是,您需要页面轮询更新的值,因为您更新工作进程中的值并不意味着浏览器将收到此新值。

我知道你说你不想使用AJAX,但要看看下面的jQuery.get(),以便更好地了解你需要做什么。

答案 1 :(得分:1)

您可以使用SignalR library来完成这项工作。

See the sample here

在给定的示例中,在服务器端dosomething()异步任务完成后,它会调用客户端notifyResult()来更新视图。

另见:Is there a way to exclude a Client from a Clients.method call in SignalR?