-
Notifications
You must be signed in to change notification settings - Fork 18
Call Task.Yield() instead of Thread.Sleep() to yield control #2
Comments
|
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. |
So this rule should actually be an analyzer that detects uses of |
👍 From me as well. |
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 |
@tmaczynski You could just make two different diagnostics.
This simplifies the analysis a bit. |
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. |
There are a few special cases with Thread.Sleep iirc. Above is from broken memory. |
@JohanLarsson You're right that
Documentation of Task.Delay(....) do not have a special case for 0 , what's more the reference source (line 5878) indicates that Semantic of Ad 2: I don’t think that I will add a special case for [1] https://msdn.microsoft.com/en-us/library/hh194873(v=vs.110).aspx :
|
@JohanLarsson |
Could you review my PR #50? |
Thread.Sleep() is not recommended. Instead, call Task.Yield() to yield control to another task.
The text was updated successfully, but these errors were encountered: