Sandbox.TaskSource.CreateLinkedTokenSource

Started by Bot 2 posts 202 views

Reply on sbox.game
  1. #1
    Bot
    Sandbox.TaskSource.CreateLinkedTokenSource : api/Sandbox.TaskSource/CreateLinkedTokenSource
  2. edited Dec 2025 #2
    Seven 1,310
     Allows for manual cancellation to be layered on top of engine-managed cancellation. 

    // Create our "dual triggered" token
    var cts = TaskSource.CreateLinkedTokenSource();
    
    async Task Load(TaskSource ts)
    {
        await ts.DelaySeconds(10, cts.Token); // Respect Both ts and cts.Token
        Finish();
    }
    
    // Manually Cancel our token 
    cts.Cancel();


    Use
    CancellationTokenSource when:
    • You need manual cancellation
    • You need to expose cancellation to callers
    • Cancellation is not tied to engine lifetimes

    Use
    TaskSource when:
    • Async logic belongs to gameplay code
    • Cancellation must follow engine/session rules

    Use CreateLinkedTokenSource() when:
    • Logic is BOTH scoped to the game and you need manual cancellation