How to stop BackgroundWorker on Form's Closing event?

后端 未结 12 1871
春和景丽
春和景丽 2020-11-21 13:55

I have a form that spawns a BackgroundWorker, that should update form\'s own textbox (on main thread), hence Invoke((Action) (...)); call.
If in Handl

12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 14:02

    Your backgroundworker should not use Invoke to update the textbox. It should ask the UI thread nicely to update the textbox using event ProgressChanged with the value to put in the textbox attached.

    During event Closed (or maybe event Closing), the UI thread remembers that the form is closed before it cancels the backgroundworker.

    Upon receiving the progressChanged the UI thread checks if the form is closed and only if not, it updates the textbox.

提交回复
热议问题