Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

BackGroundWorker with Datalayer class. Reporting Progress.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 15 Nov 2006 22:12 GMT
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
Dave Sexton - 17 Nov 2006 13:43 GMT
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();

Signature

Dave Sexton

> Hi,
> I am having trouble seeing how this bolts together.
[quoted text clipped - 13 lines]
> Thanks in Advance.
> Bob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.