locked
multiple register() on CancellationToken RRS feed

  • Question

  • I have a set of async methods that get called in a chain. Each method passes the CancellationToken it received from its parent caller to its child down the chain. When the originator cancels, everyone downstream is notified. Upon the cancel notification each method in the chain is responsible for its own mid completion cleanup. CancellationToken.Register() is perfect for this requirement but it can only register one delegate and I need each method to have its own cleanup delegate registered with this single token. Maybe I am not using the pattern correctly. How should I go about it achieving this? 
    • Moved by Forrest Guo Wednesday, December 19, 2012 11:41 AM forums restructure (From:Visual Studio Async CTP)
    • Moved by Forrest Guo Thursday, December 20, 2012 2:43 AM to right forum (From:Visual Studio General Questions)
    Wednesday, December 19, 2012 10:24 AM

Answers

  • CancellationToken.Register() is perfect for this requirement but it can only register one delegate

    Why do think so? Register() can certainly be called multiple times to register multiple delegates, there is nothing stopping you from doing that.

    For example, take following code:

    Dim cts = New CancellationTokenSource
    Dim token  = cts.Token
    token.Register(Sub() Console.WriteLine("first"))
    token.Register(Sub() Console.WriteLine("second"))
    cts.Cancel()
    

    When I run it, it prints both “second” and “first” (in this order, not sure whether that's guaranteed).

    • Marked as answer by Perry Manole Thursday, December 20, 2012 7:51 AM
    Wednesday, December 19, 2012 11:35 AM
  • Also, there is unregister. You can do that by Dispose()ing the returned CancellationTokenRegistration.
    • Marked as answer by Perry Manole Saturday, December 22, 2012 12:15 PM
    Thursday, December 20, 2012 8:00 PM

All replies

  • CancellationToken.Register() is perfect for this requirement but it can only register one delegate

    Why do think so? Register() can certainly be called multiple times to register multiple delegates, there is nothing stopping you from doing that.

    For example, take following code:

    Dim cts = New CancellationTokenSource
    Dim token  = cts.Token
    token.Register(Sub() Console.WriteLine("first"))
    token.Register(Sub() Console.WriteLine("second"))
    cts.Cancel()
    

    When I run it, it prints both “second” and “first” (in this order, not sure whether that's guaranteed).

    • Marked as answer by Perry Manole Thursday, December 20, 2012 7:51 AM
    Wednesday, December 19, 2012 11:35 AM
  • Interesting, I didn't see that in the documentation. I assumed a second call would overwrite the first since there is no unregister. I should have tested my assumption.
    • Edited by Perry Manole Thursday, December 20, 2012 7:49 AM
    Thursday, December 20, 2012 7:48 AM
  • Also, there is unregister. You can do that by Dispose()ing the returned CancellationTokenRegistration.
    • Marked as answer by Perry Manole Saturday, December 22, 2012 12:15 PM
    Thursday, December 20, 2012 8:00 PM