No video

Cancellation Culture

  Рет қаралды 3,264

Coding Tutorials

Coding Tutorials

Күн бұрын

We know how to start a task in C# and .NET, but how the heck do you stop one? With a cancellation token, that's how.
Source code available at: github.com/Jas...
Topics include:
- Multitasking
- Task.WhenAny
- Task.WhenAll
- CancellationTokenSource
- CancellationToken
- async/await

Пікірлер: 23
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Do you use cancellation tokens like this? Let me know in the comments. Source code available at: github.com/JasperKent/Cancellation-Tokens Remember to subscribe at kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA And if you liked the video, click the 👍.
@boohica
@boohica Жыл бұрын
Good luck to you sir, the C# content you are providing is one of the best I've seen on KZbin.
@eskpetri
@eskpetri Жыл бұрын
Nice to have professor like content in KZbin. It's like back in university...
@andrecadet4230
@andrecadet4230 6 ай бұрын
I left a like purely for the title
@piotrjakis1842
@piotrjakis1842 Жыл бұрын
Superb content. Keep going man 👍
@donalexey
@donalexey Жыл бұрын
Is there a way to not throw exception, but rather change task state to canceled? Like return Task.Canceled() o something similar?
@devinecataclysm6199
@devinecataclysm6199 Жыл бұрын
You could return Task.FromCanceled(ctc); which would take in the cancellation token, that should say its completed due to it being cancelled rather than faulted
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
@@devinecataclysm6199 But Task.FromCancelled() returns a Task, so it's not something you could return from a method Like PrimeCount().
@henry-js
@henry-js Жыл бұрын
Your channel is my favourite programming channel on KZbin. Do you have any videos on Authentication/Authorization/JWT?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
I'll put it on the list.
@hoangduyxiaomi
@hoangduyxiaomi Жыл бұрын
yo bro, really thankya. Big respect
@anthonyesl3860
@anthonyesl3860 Жыл бұрын
ITS REALLY WORKED LOL THANK YOU DUDE
@sandrok14
@sandrok14 Жыл бұрын
Very informative, thank you! How could you implement a method that cancels itself if is called second time? For example you called the method and inside of it, await function is doing staff. And at that moment the same method is called again from somewhere and you want to cancel previous task executing and continue with current one? so you won't get ambiguous result with 2 same tasks finishing later.
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
I think I need a bit more information to understand that.
@user-hp3wc9pr2p
@user-hp3wc9pr2p Ай бұрын
i have a question i Web API. It is mandatory to pass token from controller to repository, it there any way shorter because i want to use them in repo only ? I found a suggestion that use addScope(). It will be injected to repo instead, do you have any suggestion or any solution for this ?
@PfhorShark
@PfhorShark Жыл бұрын
do you happen to play any musical instruments?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Piano and a bit of bass guitar.
@domportera
@domportera Жыл бұрын
@@CodingTutorialsAreGo u got musician vibes for real, something about playing music and coding scratches a similar itch
@DoctorMGL
@DoctorMGL 3 ай бұрын
it can also be canceled within the for loop first line like : for (int i=0; token.IsCancellationRequested == false && i < count; i++){ ..... and i think this approach is better for performance than allowing the for() to loop > then we return it with 0 if the IsCancellationRequested success. because here we are checking for the condition before the loop even start processing
@CodingTutorialsAreGo
@CodingTutorialsAreGo 3 ай бұрын
Which is exactly what you don't want to do. You do not want to return 0 because that is not the correct result. And if the cancellation happened later, you would get a non-zero value that was still not the correct result. How would the client code know? You need to throw an exception to indicate that the calculation did not complete.
@DoctorMGL
@DoctorMGL 3 ай бұрын
​@@CodingTutorialsAreGo , good point, thanks for bringing my attention to this , and what i actually mean depend on the case . if the user ( Cancel the task ) vs ( he EXITs the Window ) , when Existing there is no longer " telling the user where did he stop " in this case we force the Cancellation at the closest point and Dispose everything . unless you want the user to continue his task from where did he stop if he launch the ( application / task ) again . if so that's another topic related to Cashing, something you may need in applications like "Internet download manager" where you want the user to have the ability to keep downloading from the same percentage he stop at even if the application exists or crashed .
@manmohanmundhraa3087
@manmohanmundhraa3087 Жыл бұрын
can we cancel all task which are using same token ** within a particular task ** ?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Yes, when a cancellation is requested, it will be received by all tasks which have been passed the token. But it's still down to the task itself to decide when and if it aborts.
Tracking Progress
18:35
Coding Tutorials
Рет қаралды 2,1 М.
Cancellation Token in .NET | Exploring C# and DOTNET
18:42
Rahul Nath
Рет қаралды 5 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 14 МЛН
UNO!
00:18
БРУНО
Рет қаралды 5 МЛН
7 Days Stranded In A Cave
17:59
MrBeast
Рет қаралды 91 МЛН
The Giant sleep in the town 👹🛏️🏡
00:24
Construction Site
Рет қаралды 20 МЛН
Blazor RenderFragment
23:11
Coding Tutorials
Рет қаралды 1,7 М.
Parallelization and PLINQ
20:43
Coding Tutorials
Рет қаралды 6 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 151 М.
C# 12 Collection Expressions and the Spread Operator
25:27
Coding Tutorials
Рет қаралды 1,9 М.
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 257 М.
8 await async mistakes that you SHOULD avoid in .NET
21:13
Nick Chapsas
Рет қаралды 311 М.
No, Flexbox isn't "good enough"
9:18
Kevin Powell
Рет қаралды 39 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 14 МЛН