【问题标题】:ASP.NET Asynchronous label updateASP.NET 异步标签更新
【发布时间】:2014-03-18 03:25:00
【问题描述】:

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

这是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!";
            }
        }
    }
}

Background Workers 不是解决这个问题的正确方法吗?

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

【问题讨论】:

    标签: c# asp.net asynchronous


    【解决方案1】:

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

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

    我知道您说过您不想使用 AJAX,但请查看以下jQuery.get() 以更好地了解您需要做什么。

    【讨论】:

      【解决方案2】:

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

      See the sample here

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-01
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多