5 useful extensions for Task<T> in .NET #53
Replies: 8 comments 6 replies
-
|
Very nice article, thank you! Is there a reason why, in section 4, there are no ConfigureAwait(false) ? |
Beta Was this translation helpful? Give feedback.
-
|
Can we use Task.WaitAsync instead of WithTimeout in section 4? |
Beta Was this translation helpful? Give feedback.
-
|
In section 4, I think you can also do
await task.ConfigureAwait(false);
Btw really annoying, I know...
…On Wed, Apr 12, 2023 at 7:32 AM Steven Giesel ***@***.***> wrote:
Yes - since .NET 6 you can use WaitAsync. I will amend the post! Thanks
—
Reply to this email directly, view it on GitHub
<#53 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEN72MYVMUAIEWSRGHJ7DHLXAY5ADANCNFSM6AAAAAAW2HHGK4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***
com>
--
*Damiano Curia*
Swiss mobile: +41 76 50 40 60 2
Italian mobile: +39 348/360.25.73
skype: curia.damiano
email: ***@***.***
www: http://curia.me
|
Beta Was this translation helpful? Give feedback.
-
|
In case of section 2 (retry) wouldn't you want the Task continuing on the original context (if any)? Now, the first execution will run on within the original context, but the retries will be scheduled outside of the original context (somewhere in the threadpool). In asp.net (core) you won't notice this as there is no synchronization context (https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html), but in a UI application or old-school asp.net this could cause issues. |
Beta Was this translation helpful? Give feedback.
-
|
Re 4. in this implementation will the original (potentailly long running) Task be cancelled after specified timeout or not? |
Beta Was this translation helpful? Give feedback.
-
|
section 4... be careful about using Task.Delay. Under the hood it sets up a Timer which will only be disposed on completion of the delay... whether or not you are still awaiting the result. If the delay is used in a loop, or relatively long lasting, you may end up using more resources than you would expect. To my knowledge, there is no wait to dispose this internal timer if you no longer want to wait. However there are no reasonable alternatives though. |
Beta Was this translation helpful? Give feedback.
-
|
In section 2. How about catching OperationCanceledException and prevent from retrying? |
Beta Was this translation helpful? Give feedback.
-
|
Great extensions! But, I run into this when trying to build #4: (.NET 6) https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1503?f1url=%3FappId%3Droslyn%26k%3Dk(CS1503) Severity Code Description Project File Line Suppression State |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
5 useful extensions for Task in .NET
In this short blog post, I will show you 5 useful extensions for Task in .NET. We will build them as extension methods, so there are easy to use.
On top, I will show a small example of how to use them. So let's go!
https://steven-giesel.com/blogPost/d38e70b4-6f36-41ff-8011-b0b0d1f54f6e
Beta Was this translation helpful? Give feedback.
All reactions