DEV Community

Discussion on: Timers in .NET

Collapse
 
solrevdev profile image
John Smith

Hi Katie,

Take a look at this. hitting ctrl-c while its running or in the 5-second DoWork method will cancel gracefully and hopefully do what you want.

I plan to blog about it in more detail later but this should help.

Gist here

gist.github.com/solrevdev/f28ab813...

Collapse
 
katnel20 profile image
Katie Nelson

Thanks John. I see now the running timer callback is buried several levels down. Makes things a bit more complex, but I understand what you did.

Thread Thread
 
solrevdev profile image
John Smith

Ahh yes, the timer's TimerCallback method signature needed to be void DoWork(object state) hence needing another method call that uses async Task accepting a CancellationToken.

But looking at it again I think that DoWorkAsync and RunJobAsync could just be the one method.

I'll take another look later and will try and explain more in another blog post.

Thread Thread
 
katnel20 profile image
Katie Nelson

Great! Looking forward to it.