How to report progress from within a class to a BackgroundWorker?

后端 未结 4 727
Happy的楠姐
Happy的楠姐 2020-12-09 11:31

My WinForm calls a class which performs some copying actions. I\'d like to show the progress of this on a form.

I\'d like to use the Backgroundworker, but I don\'t k

4条回答
  •  生来不讨喜
    2020-12-09 12:02

    Everything you need to know about BackgroundWorker is on msdn.

    As it says in the article:

    To receive notifications of progress updates, handle the ProgressChanged event.


    Update:

    Having read Martijn's supplementary questions, and given that he has a class which hitherto has been doing his work, presumably on the foreground thread, I'd add the following:

    • The worker class has responsibility for the work, so it also has responsibility for reporting on its progress. The fact that it spawns a background thread to do the work is not the concern of the Form.

    • So, I'd be inclined to have the class set up the BGW, and handle its ProgressChanged events, and then raise its own events (on the foreground thread) to which the form itself could then subscribe. I do a ton of WinForms coding using this technique and it works fine.

    The alternative would be to expose the BGW as a public property of the worker class, and have the form handle its events directly. But I don't like this so much, since it makes the form dependent on the implementation of the worker class. This is generally A Bad Thing.

提交回复
热议问题