Comments (12)
await Task.Delay(...)
is a drop-in replacement for Thread.Sleep()
within an async
method. I believe Task.Yield
is used for other purposes.
from asyncusageanalyzers.
Task.Yield() returns a task that if you ask it if it is finished will return false, and then finish immediately. await will check if a Task is already finished and it will not give the control back to the caller if it is to reduce the number of context switches. So Task.Yield() will make sure the method returns and a context switch is going to happen.
from asyncusageanalyzers.
So this rule should actually be an analyzer that detects uses of Thread.Sleep(...)
in an asynchronous method, and replaces them with await Task.Delay(...)
. I'm not sure what to call it, but overall it gets a π from me.
from asyncusageanalyzers.
π From me as well.
from asyncusageanalyzers.
I plan to implement this analyzer after I finish implementing issue 1562 from StyleCopAnalyzers.
I think that this diagnostic and code fix should be a little bit more complex since async code might call other methods which use Thread.Sleep(...)
and if those methods are private and are used only from async methods, they should be fixed as well.
from asyncusageanalyzers.
@tmaczynski You could just make two different diagnostics.
Thread.Sleep
should not be used inside of asynchronous methodsThread.Sleep
should not be used anywhere
This simplifies the analysis a bit.
from asyncusageanalyzers.
@sharwell I've created a pull request #50 with two diagnostics as you suggested.
from asyncusageanalyzers.
Could you change the tag of this issue - it should be "pull request" instead of "up for grabs". I looking forward to have this PR reviewed - extension methods that I added might help me with issue #1.
from asyncusageanalyzers.
There are a few special cases with Thread.Sleep iirc.
Thread.Sleep(0) is similar to Task.Yield.
Thread.Sleep(1) is also similar to Task.Yield but forces context switch.
Above is from broken memory.
from asyncusageanalyzers.
@JohanLarsson
Ad 1:
You're right that Thread.Sleep(0)
is similar to await Task.Yield()
, not await Task.Delay(0)
. MSDN doc of Sleep method says that:
If the value of the millisecondsTimeout argument is zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run.
Documentation of Task.Delay(....) do not have a special case for 0 , what's more the reference source (line 5878) indicates that Task.Delay(0, cancellationToken)
returns Task.CompletedTask
which means that awaiting that method can execute synchronously.
Semantic of await Task.Yield()
seems to be close to that of Thread.Sleep(0)
. I guess that refactoring Thread.Sleep(0)
to await Task.Yield()
might introduce some regression in some edge cases (e.g. when code relies on affinity of the code that is scheduled after sleep), but I think that such refactoring is correct in 99% of cases.
Ad 2:
I donβt think that Thread.Sleep(1)
is another special case - itβs simply the shortest possible sleep (far below default length of a Window timeslice which is ~15milliseconds), but await Task.Delay(1)
works in similar way [source: 1].
I will add a special case for Thread.Sleep(0)
shortly. In the meantime, more feedback is welcomed.
[1] https://msdn.microsoft.com/en-us/library/hh194873(v=vs.110).aspx :
This method depends on the system clock. This means that the time delay will approximately equal the resolution of the system clock if the millisecondsDelay argument is less than the resolution of the system clock, which is approximately 15 milliseconds on Windows systems.
from asyncusageanalyzers.
@JohanLarsson
I added added code which changes Thread.Sleep(0)
and Thread.Sleep(TimeSpan.Zero)
to await Task.Yield()
.
from asyncusageanalyzers.
Could you review my PR #50?
from asyncusageanalyzers.
Related Issues (20)
- New rule proposal: Propagate cancellation tokens HOT 7
- Update IncludeCancellationParameter analyzer to check overloads
- Update IncludeCancellationParameter analyzer to cache context lookups
- Document IncludeCancellationParameter diagnostic
- Support Supression attribute HOT 3
- Update documentation to include [SuppressMessage] examples HOT 2
- Code fixes from AsyncUsageAnalyzers.CodeFixes don't work in Visual Studio
- Don't provide code fixes for Visual Basic in UseConfigureAwaitCodeFixProvider and DontUseThreadSleepCodeUniversalCodeFixProvider
- Suppress async void nag in asserts. HOT 9
- Add special rule for async void eventhandlers. HOT 1
- Await inside usings. HOT 4
- Refactor issue for ignoring a rule HOT 1
- Support Main with async
- Bug: Analyser not raising warnings for local functions not ending with "Async". HOT 1
- ValueTask<long[]> DeleteAsync
- AvoidAsyncSuffix misfires on ValueTask<T> returning methods HOT 1
- Treat types marked with AsyncMethodBuilderAttribute as asynchronous
- Treat methods with awaitable return types as asynchronous
- The non-async method for subclass return a ValueTask report AvoidAsyncSuffix
- New rule proposal: Called methods that are not marked async but return a task should be awaited HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncusageanalyzers.