The first thing you need to do is separate the feedback and cancellation ideas. That is, you have a feedback mechanism and a cancellation mechanism, but they are completely unrelated. If you can separate these two concepts, then it will become easier for you to cope, and it will also work better.
You want to provide feedback at some interval, which implies a timer. And instead of polling, to see if a cancellation is being requested, you can wait using the wait descriptor.
Something like:
int i = 0; using (System.Threading.Timer tmr = new System.Threading.Timer((s) => { if (Status != null) Status(this, new StatusEventArgs(i)); ++i; }, null, 1000, 1000)) { token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10))); }
I assume that you get a CancellationToken from another place (i.e. it passed, or you define it, and others can affect its state).
Jim mischel
source share