Relative Content

Tag Archive for cancellation-tokencancellationtokensource

Using Cancellation Tokens without leaking memory

I have a need for an application with one global CancellationTokenSource. This CancellationTokenSource is to be triggered only when the user closes the app.
The app ‘does work’ and for each work item I want to pass in a CancellationToken from the global CancellationTokenSource. The idea is that when the user
wants to exit the app, any running work items that they started will be cancelled before the app closes. The question is, how can I implement this without
creating a memory leak. Below is a sample of this idea that seems to be leaking memory: