Home Will a task ever be interrupted by garbage collection if there are no references to it?
Reply: 0

Will a task ever be interrupted by garbage collection if there are no references to it?

user767
1#
user767 Published in June 19, 2018, 2:24 pm

Sanity-check here. Suppose I start a task but I don't await it and I don't store a reference to it. In other words, I just run:

async Task PerformLongOperation() {
    await Task.Delay(10 * 1000);
    Debug.WriteLine("All done!");
}

void DoSomething() {
    // Kick off the operation and allow it to complete when it does.
    PerformLongOperation();
}

Clearly, without keeping a reference to the Task returned by PerformLongOperation(), I have no way of knowing if and when it completes. But suppose that that's not necessary in certain instances.

Is there any danger of garbage collection noticing the task running with no references to it, and cancelling it and cleaning it up?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.562065 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO