代码之家  ›  专栏  ›  技术社区  ›  Fourat

做繁重的工作,汇报工作进展asp.net网页

  •  1
  • Fourat  · 技术社区  · 6 年前

    Task 应该是executes,我希望任务异步执行,并将其进度报告给 File

    <asp:UpdatePanel runat="server" ID="upPage">
        <ContentTemplate>
            <div class="jumbotron">
                <asp:Button runat="server" ID="BtnExecute" CssClass="btn btn-primary btn-lg" Text="Execute Heavy Task" OnClick="BtnExecute_Click" />
                <asp:Label runat="server" ID="LblStatus" Text="Ready...."/>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    代码隐藏:

    public partial class _Default : Page
    {
        public Progress<int> HeavyTaskProgress { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void BtnExecute_Click(object sender, EventArgs e)
        {
            HeavyTaskProgress = new Progress<int>();
            HeavyTaskProgress.ProgressChanged += Progress_ProgressChanged;
            Task.Run(() => DoProcessing(HeavyTaskProgress)).ContinueWith(t => ProcessingFinished(t));
            LblStatus.Text = "Task is launched.... Please wait";
        }
        private void Progress_ProgressChanged(object sender, int e)
        {
            string message = string.Format("progress at {0}%{1}", e, Environment.NewLine);
            System.IO.File.AppendAllText(@"d:\progress.txt", message);
        }
        private void DoProcessing(IProgress<int> progress)
        {
            for (int i = 0; i < 10; ++i)
            {
                Thread.Sleep(1000); // CPU-bound work
                if (progress != null)
                    progress.Report(i);
            }
        }
        private void ProcessingFinished(Task t)
        {
            System.IO.File.AppendAllText(@"d:\progress.txt", "Task done !! " + t.Status);
        }
    }
    

    d:\progress.txt Progress_ProgressChanged 是不是坏了)。我想这可能是因为 进度\u进度已更改 DoProcessing 在不同的线程中执行,但我不知道如何修复它。

    谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Jacobski    6 年前

    我认为您的问题是,在引发事件时,处理HTTP请求的线程被终止。


    如果你想要一些定期的反馈发送给用户,你可以看看信号器。

    public class DoingProcessing
    {
        public Progress<int> HeavyTaskProgress { get; set; }
    
        private void Progress_ProgressChanged(object sender, int e)
        {
            string message = string.Format("progress at {0}%{1}", e, Environment.NewLine);
            System.IO.File.AppendAllText(@"d:\progress.txt", message);
        }
    
        public void DoProcessing()
        {
            HeavyTaskProgress = new Progress<int>();
            HeavyTaskProgress.ProgressChanged += Progress_ProgressChanged;
            DoProcessing(HeavyTaskProgress);
        }
        private void DoProcessing(IProgress<int> progress)
        {
            for (int i = 1; i <= 10; ++i)
            {
                Thread.Sleep(1000); // CPU-bound work
                if (progress != null)
                    progress.Report(i*10);
            }
        }
    
        public void ProcessingFinished(Task t)
        {
            System.IO.File.AppendAllText(@"d:\progress.txt", "Task done !! " + t.Status);
        }
        public DoingProcessing()
        {
    
        }
    }