简单的BackgroundWorker不是网页上更新标签 [英] Simple BackgroundWorker is not updating label on web page

查看:110
本文介绍了简单的BackgroundWorker不是网页上更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一块简单的code从这个有用
<一href=\"http://stackoverflow.com/questions/363377/how-do-i-run-a-simple-bit-of-$c$c-in-a-new-thread\">post

它使用一个按钮和一个标签,标签应报告10%完成...20%完成...等。

当我调试时,code被击中,但我的标签没有更新的浏览器。

我曾尝试与&安培;无更新板,具有和不具有母版

 保护无效btnStartThread_Click(对象发件人,EventArgs的发送)
    {
        BackgroundWorker的体重=新的BackgroundWorker();        //这使得我们的工作人员汇报工作进展情况时
        bw.WorkerReportsProgress = TRUE;        //什么在后台线程做
        bw.DoWork + =新DoWorkEventHandler(委托(对象o,DoWorkEventArgs参数)
        {
            BackgroundWorker的B = 0作为BackgroundWorker的;            //做一些简单的处理,持续10秒
            的for(int i = 1; I&LT; = 10;我++)
            {
                //报告百分比的进展
                b.ReportProgress(ⅰ* 10);
                Thread.sleep代码(1000);
            }
        });        //当进度改变做什么(更新例如进度条)
        bw.ProgressChanged + =新ProgressChangedEventHandler(委托(对象o,ProgressChangedEventArgs参数)
        {
            label1.Text =的String.Format({0}%完成,args.ProgressPercentage);
        });
        //当工人完成了它的任务做什么(通知用户)
        bw.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(
        委托(对象o,RunWorkerCompletedEventArgs参数)
        {
            label1.Text =完了!
        });        bw.RunWorkerAsync();


解决方案

您可能会做长轮询来获得更新的结果。就个人而言我不会在Web应用程序中使用后台工作。另外,还要考虑看SignalR,服务器发送事件和异步并等待或任务并行库。

I have used a piece of simple code from this helpful post

It uses a button and a label, the label should report "10% completed"... "20% completed"... and so on.

When I debug, the code is getting hit, but my label is not updating on the browser.

I have tried with & without update panels, with and without a masterpage.

protected void btnStartThread_Click(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();

        // this allows our worker to report progress during work
        bw.WorkerReportsProgress = true;

        // what to do in the background thread
        bw.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs args)
        {
            BackgroundWorker b = o as BackgroundWorker;

            // do some simple processing for 10 seconds
            for (int i = 1; i <= 10; i++)
            {
                // report the progress in percent
                b.ReportProgress(i * 10);
                Thread.Sleep(1000);
            }
        });



        // what to do when progress changed (update the progress bar for example)
        bw.ProgressChanged += new ProgressChangedEventHandler(delegate(object o, ProgressChangedEventArgs args)
        {
            label1.Text = string.Format("{0}% Completed", args.ProgressPercentage);
        });


        // what to do when worker completes its task (notify the user)
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        delegate(object o, RunWorkerCompletedEventArgs args)
        {
            label1.Text = "Finished!";
        });

        bw.RunWorkerAsync();

解决方案

You could potentially do long polling to get your updated result. Personally I wouldnt use a background worker in a Web application. Also, consider looking at SignalR, Server Sent Events and Async and Await or the Task Parallel Library.

这篇关于简单的BackgroundWorker不是网页上更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆