public class Cls
{
public object Obj { get; set; }
public async void Func()
{
Task.Run(() =>
{
Thread.Sleep(999999999);
this.Obj = new { };
});
}
}
public class Program
{
public static void Main(string[] args)
{
new Cls().Func();
}
}
Please consider the above codes and neglect if it makes sense first. In the above case, I did not store instance of Cls into any variable, seems that nothing is referencing that instance and it would be GC. However, there is a Task.Run() in side Func. The callback function of the Task make a reference to the instance's Obj property. May I will it still be collected by GC in such case?
I am asking this question because in Microsoft doc of SignalR it stated
Use await when calling asynchronous methods that depend on the hub staying alive.
I don't understand how come the Hub will not still alive as long as something inside Clients.All.SendAsync is referencing to the Hub itself...
Thanks.
Clswill not be garbage collected while the task is alive. Just the fact your are callingthisprevents it, among other things...Hubclass. After that method call is over SignalR willDisposethatHubinstance. So if you don'tawaitanasynccall it will only start it and not wait for completion, then callDisposeon theHubinstance. ThisDisposeis directly called from the SignalR framework so has nothing to do with GarbageCollection and having references to aHub.Hub, however it will have been disposed and therefor not 'alive' anymore"