Can running object be garbage collected?

Posted by Kugel on Stack Overflow See other posts from Stack Overflow or by Kugel
Published on 2010-04-30T14:37:01Z Indexed on 2010/04/30 14:47 UTC
Read the original article Hit count: 259

Filed under:

I have a simple class:

public class Runner
{
    public void RunAndForget(RunDelegate method)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(Run), method);
    }

    private void Run(object o)
    {
        ((RunDelegate )o).Invoke();
    }
}

And if I use this like so:

private void RunSomethingASync()
{
    Runner runner = new Runner();
    runner.FireAndForget(new RunDelegate(Something));
}

Is there any danger using it like this? My C++ guts tell me that runner object should be destroyed after RunSomethingASync is finished. Am I right? What happens then to the method running on different thread?

Or perhaps it is other way around and runner will not be collected? That would be a problem considering I may call RunSomethingASync() many times.

© Stack Overflow or respective owner

Related posts about c#