Cancellation Culture

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ย. 2024
  • 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  ปีที่แล้ว +2

    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 th-cam.com/channels/qWQzlUDdllnLmtgfSgYTCA.html
    And if you liked the video, click the 👍.

  • @boohica
    @boohica ปีที่แล้ว +5

    Good luck to you sir, the C# content you are providing is one of the best I've seen on TH-cam.

  • @eskpetri
    @eskpetri ปีที่แล้ว +1

    Nice to have professor like content in TH-cam. It's like back in university...

  • @henry-js
    @henry-js ปีที่แล้ว +1

    Your channel is my favourite programming channel on TH-cam. Do you have any videos on Authentication/Authorization/JWT?

  • @andrecadet4230
    @andrecadet4230 7 หลายเดือนก่อน +1

    I left a like purely for the title

  • @donalexey
    @donalexey ปีที่แล้ว +3

    Is there a way to not throw exception, but rather change task state to canceled? Like return Task.Canceled() o something similar?

    • @devinecataclysm6199
      @devinecataclysm6199 ปีที่แล้ว +1

      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  ปีที่แล้ว +1

      @@devinecataclysm6199 But Task.FromCancelled() returns a Task, so it's not something you could return from a method Like PrimeCount().

  • @sandrok14
    @sandrok14 ปีที่แล้ว +1

    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.

  • @piotrjakis1842
    @piotrjakis1842 ปีที่แล้ว +1

    Superb content. Keep going man 👍

  • @tantruongthe-z7o
    @tantruongthe-z7o 2 หลายเดือนก่อน

    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 ?

  • @anthonyesl3860
    @anthonyesl3860 ปีที่แล้ว +1

    ITS REALLY WORKED LOL THANK YOU DUDE

  • @PfhorShark
    @PfhorShark ปีที่แล้ว +2

    do you happen to play any musical instruments?

    • @CodingTutorialsAreGo
      @CodingTutorialsAreGo  ปีที่แล้ว +2

      Piano and a bit of bass guitar.

    • @domportera
      @domportera ปีที่แล้ว +1

      @@CodingTutorialsAreGo u got musician vibes for real, something about playing music and coding scratches a similar itch

  • @hoangduyxiaomi
    @hoangduyxiaomi ปีที่แล้ว +1

    yo bro, really thankya. Big respect

  • @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.

  • @DoctorMGL
    @DoctorMGL 4 หลายเดือนก่อน

    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  4 หลายเดือนก่อน +1

      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 4 หลายเดือนก่อน

      ​@@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 .