I have a background worker which updates the GUI on a regular basis via ReportProgress.
The update occurs at regular intervals, every 5 seconds for example, or it c
You're going down the wrong path.
Sleep()
on any thread. Certainly not for more than a few ms. Sleep(1)
in a busy-wait for 5 seconds is a big waste of CPU. You are hurting your other threads. At the very least consider upping it to Sleep(100)
or so. Find the max delay you will allow for your Cancel. As a solution: Use a Timer. You have a periodical task, use the right tool.
Use System.Threading.Timer
for ThreadPool based background work. Update the GUI through Dispatcher.Invoke().
Use the Dispatcher.Timer
to execute small (under 0.1 sec) portions of work on the Main thread. You can directly update the GUI.