By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

BackGroundWorker with Datalayer class. Reporting Progress.

P: n/a
Bob
Hi,
I am having trouble seeing how this bolts together.
The UI starts a process which involves a long running database update.
All Database activity is handled by a class called DT.

DT has a progress event.

So I added a bw to the form. The Dowork Calls a method which instantiates a
DT and calls its Dataprocessing method.

I can't see how to bubble progress up from the DT event to the Form.

If I am on the right track here , the bw will handle the DT event (How?) ,
then it will raise its own ProgressChanged Event. The UI handles the bw
ProgressChanged Event and updates the progress bar.
Thanks in Advance.
Bob
Nov 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Bob,

1. You must set WorkerReportsProgress to true on the BackgroundWorker object.

2. Register an event handler with the ProgressChanged event on the
BackgroundWorker. The handler will be invoked on the UI thread, so you can
update a ProgressBar control or whatever you'd like.

3. In the DoWork event handler, register an event handler for the progress
changed event of the DT object that is created.

4. In the event handler for the DT progress change event, call the
ReportProgress method on the BackgroundWorker

Anonymous methods can make this process much easier for you if you don't have
a global variable declared for the BackgroundWorker or if you create a new
BackgroundWorker each time the process must run:

BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;

// ProgressBar control
progress.Value = 0;

worker.ProgressChanged += delegate(object sender1, ProgressChangedEventArgs
e1)
{
// update the progress bar
progress.Value = e1.ProgressPercentage;
};

worker.DoWork += delegate(object sender1, DoWorkEventArgs e1)
{
// here you would create your DT object
// here you would register an event handler - another anonymous
// method could be used so you have access to the "worker"
// variable in the event handler

// as an example, I'll just update the progress directly:

for (int i = 1; i <= 10; i++)
{
System.Threading.Thread.Sleep(100);

worker.ReportProgress(i * 10);
}

worker.ReportProgress(100);
};

worker.RunWorkerAsync();

--
Dave Sexton

"Bob" <bo*@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,
I am having trouble seeing how this bolts together.
The UI starts a process which involves a long running database update.
All Database activity is handled by a class called DT.

DT has a progress event.

So I added a bw to the form. The Dowork Calls a method which instantiates a
DT and calls its Dataprocessing method.

I can't see how to bubble progress up from the DT event to the Form.

If I am on the right track here , the bw will handle the DT event (How?) ,
then it will raise its own ProgressChanged Event. The UI handles the bw
ProgressChanged Event and updates the progress bar.
Thanks in Advance.
Bob


Nov 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.