BackGroundWorker with Datalayer class. Reporting Progress.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    BackGroundWorker with Datalayer class. Reporting Progress.

    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

    #2
    Re: BackGroundWorke r with Datalayer class. Reporting Progress.

    Hi Bob,

    1. You must set WorkerReportsPr ogress to true on the BackgroundWorke r object.

    2. Register an event handler with the ProgressChanged event on the
    BackgroundWorke r. 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 BackgroundWorke r

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

    BackgroundWorke r worker = new BackgroundWorke r();
    worker.WorkerRe portsProgress = true;

    // ProgressBar control
    progress.Value = 0;

    worker.Progress Changed += delegate(object sender1, ProgressChanged EventArgs
    e1)
    {
    // update the progress bar
    progress.Value = e1.ProgressPerc entage;
    };

    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.Threadin g.Thread.Sleep( 100);

    worker.ReportPr ogress(i * 10);
    }

    worker.ReportPr ogress(100);
    };

    worker.RunWorke rAsync();

    --
    Dave Sexton

    "Bob" <bob@nowhere.co mwrote in message
    news:%23bR22MQC HHA.4472@TK2MSF TNGP03.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
    >
    >

    Comment

    Working...