470,855 Members | 1,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

BackgroundWorker.ProgressChanged Event

WB
Hi,

My Windows application has a time-consuming operation which I'd like to run
on a separate thread so that the UI can remain responsive.

I'm thinking of using the BackgroundWorker class and have referred to a
relevant MSDN article at:

http://msdn2.microsoft.com/en-us/lib...undworker.aspx

However, I notice that "ProgressChangedEventArgs" of the
"backgroundWorker1_ProgressChanged" event handler only has a parameter
"ProgressPercentage". When I give progress updates I'd like to give more
information by, for example, displaying text in a text box (eg. "Found 3
pending customer orders"), instead of simply incrementing a progress bar. Now
how can I accomplish this?

ywb.
Apr 27 '06 #1
2 4486
WB <WB@discussions.microsoft.com> wrote:
My Windows application has a time-consuming operation which I'd like to run
on a separate thread so that the UI can remain responsive.
[snip]
When I give progress updates I'd like to give more
information by, for example, displaying text in a text box (eg. "Found 3
pending customer orders"), instead of simply incrementing a progress bar. Now
how can I accomplish this?


You'll have to update the textbox on the main UI thread. You can marshal
the action over to the UI thread with ISynchronizeInvoke.Invoke /
Control.Invoke. The easiest way to send all the code across is via an
anonymous delegate. For example:

---8<---
using System;
using System.Windows.Forms;
using System.Threading;

class App
{
delegate void Method();

static void Main()
{
Form f = new Form();
f.Text = "Form";

ThreadPool.QueueUserWorkItem(delegate
{
Thread.Sleep(2000);
f.Invoke((Method) delegate
{
f.Text = "Changed!";
});
});

Application.Run(f);
}
}
--->8---

-- Barry
Apr 27 '06 #2
On Thu, 27 Apr 2006 13:53:02 -0700, WB wrote:
However, I notice that "ProgressChangedEventArgs" of the
"backgroundWorker1_ProgressChanged" event handler only has a parameter
"ProgressPercentage". When I give progress updates I'd like to give more
information by, for example, displaying text in a text box (eg. "Found 3
pending customer orders"), instead of simply incrementing a progress bar. Now
how can I accomplish this?


ProgressChangedEventArgs has another property: UserState. When calling the
ReportProgress method, you can use the userState parameter to pass a custom
object that contains the information that you want to display to the user.
In the ProgressChanged event handler, you can then cast the UserState to
the type of your custom object and retrieve the info.
Apr 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by =?Utf-8?B?SXNobWFlbA==?= | last post: by
7 posts views Thread by =?Utf-8?B?SGVucnk5OQ==?= | last post: by
3 posts views Thread by vulpes | last post: by
4 posts views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.