So you probably need a significantly different implementation. Consider this:
public class MyForm { private BackgroundWorker _worker; public MyForm() { _worker = new BackgroundWorker(); _worker.DoWork += (s, args) => { var timer = Stopwatch().StartNew(); do {
and then when you want to run it:
_worker.RunWorkerAsync();
However, you can make it even more reliable. You can pass the time as follows:
_worker.RunWorkerAsync(60000);
and then in the DoWork handler do the following:
while (timer.ElapsedMilliseconds < (int)args.Argument)
In addition, with BackgroundWorker you can support undo. Just set the WorkerSupportsCancellation flag to true , and then able to do the following:
while (timer.ElapsedMilliseconds < (int)args.Argument && !_worker.CancellationPending)
therefore, if necessary, you can do this:
_worker.CancelAsync();
source share