Visual Studio 2022 - Unexpected Memory Leak in WinForms Application with BackgroundWorker
I'm trying to configure I'm working with an unexpected memory leak in my WinForms application that's using `BackgroundWorker` to perform some long-running tasks. After running the application for a while, the memory usage gradually increases, which eventually leads to performance optimization and crashes. I've set up the `BackgroundWorker` like this: ```csharp BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, e) => { // Simulating a long-running task for (int i = 0; i < 100000; i++) { // Perform some intensive processing } }; worker.RunWorkerCompleted += (sender, e) => { // Update UI or handle completion }; worker.RunWorkerAsync(); ``` I've tried disposing of the `BackgroundWorker` after completion, but it doesn't seem to help. I've also checked for any event handler leaks by unsubscribing from events, yet the memory usage keeps increasing. I ran the `.NET Memory Profiler` and found that there are several instances of `Task` and `CancellationTokenSource` that are not being collected, which I suspect are related to my use of `BackgroundWorker`. Could there be something I'm missing in my implementation or in how `BackgroundWorker` manages resources? I've looked into using `async`/`await` patterns as a potential solution, but I would prefer sticking with `BackgroundWorker` for now if possible. Any insights or debugging tips would be greatly appreciated! I recently upgraded to C# stable. Any pointers in the right direction?