Click here to Skip to main content
15,887,135 members
Articles / General Programming / Threads

Use CancellationToken - not Thread.Sleep

Rate me:
Please Sign up or sign in to vote.
4.95/5 (17 votes)
13 May 2020CPOL2 min read 24.5K   221   10  
Thread.Sleep cannot be cancelled. To inject a pause, consider using the Cancellation Token instead.
It is quite common to discover a need to inject a pause in the code execution, a remote target or file may not be ready at first glance, so you back off for a period and try again, repeating until the target is ready or the process is cancelled. In such a case, it is tempting to put in a Thread.Sleep(n) - but this will not respond to a cancellation trigger, so why not use the CancellationToken instead?

Views

Daily Counts

Downloads

Weekly Counts

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions