1

I can't get this BackgroundWorker to work for me. I'm using m3rLinEz's example from here

The problem is that the GUI does not respond and the percentage is not updating.

I'm using a master page and I've set async="true" in the header of the content page.
Am I missing something else?

ASPX Code:

<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnClick_Go" />&nbsp;
<asp:Label runat="server" id="textUpdate" text="0%" />

code behind

protected void btnClick_Go(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)
    {
        textUpdate.Text = string.Format("{0}%", args.ProgressPercentage);
    });

    // what to do when worker completes its task (notify the user)
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
    delegate(object o, RunWorkerCompletedEventArgs args)
    {
        lblSuccess.Visible = true;
    });

    bw.RunWorkerAsync();
}
Community
  • 1
  • 1
Niklas
  • 13,005
  • 23
  • 79
  • 119

1 Answers1

2

BackgroundWorker is usually used with client-side UIs - WPF, WinForms etc.

In your code, you're trying to update the UI after the response has been sent back to the client. How do you expect that to work without a later request from the client to the server?

When it comes to web applications, you'll need to use AJAX to keep updating the UI. There may well be nice ways of making that AJAX easy to manage, but you can't just use BackgroundWorker on the server side and hope it'll all work.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So could I start this background worker, have it updating a session variable, and then have an updatepanel polling that session object on the server? Or can't I use background worker at all here? – Niklas Jan 30 '13 at 10:21
  • @Niklas: Well you lose most of the benefits that `BackgroundWorker` providers. It sounds like you basically want to put something on the thread pool. Personally I'm not sure I'd use a session as such - I'd create an ID for "the task I've started" and update the state of that task in something persistent (probably a database) periodically. – Jon Skeet Jan 30 '13 at 10:24
  • Ok, I'll start looking at threads...again =) Thanks! This saved some unnecessary trial and error. – Niklas Jan 30 '13 at 10:28
  • @JonSkeet: `BGW` is based on `SynchronizationContext`, so it does work in ASP.NET. But as you stated, HTTP only gives you one response for one request. @Niklas: It's the polling you need. Either do long polling or repeated polling yourself or see if you can get something like SignalR working (not sure if it supports .NET 3.5). If you add a background thread to ASP.NET, you're [living dangerously](http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx). – Stephen Cleary Jan 30 '13 at 17:43
  • @StephenCleary: Yes, it *works* - but it doesn't offer any significant benefit here, is my point :) – Jon Skeet Jan 30 '13 at 17:54