Czy .Net Garbage Zbierze obiekt, do którego nie ma odniesienia, ale ma wątek, który działa?

Mam następujący kod (obniżony dla czytelności):

Klasa główna:

<code>public StartProcess()
{
    Thinker th = new Thinker();
    th.DoneThinking += new Thinker.ProcessingFinished(ThinkerFinished);
    th.StartThinking();
}

void ThinkerFinished()
{
    Console.WriteLine("Thinker finished");
}
</code>

Klasa myślicieli:

<code>public class Thinker
{
    private System.Timers.Timer t;

    public delegate void ProcessingFinished();
    public event ProcessingFinished DoneThinking;

    BackgroundWorker backgroundThread;

    public Thinker() { }

    public StartThinking()
    {
        t = new System.Timers.Timer(5000);    // 5 second timer
        t.AutoReset = false;
        t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
        t.Start();

        // start a background thread to do the thinking
        backgroundThread = new BackgroundWorker();
        backgroundThread.DoWork += new DoWorkEventHandler(BgThread_DoWork);
        backgroundThread.RunWorkerAsync();
    }

    void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        DoneThinking();
    }

    BgThread_DoWork(object sender, DoWorkEventArgs e)
    {
        // work in here should go for much less than 5 seconds
        // it will die if it doesn't

        t.Stop();
        DoneThinking();
    }
}
</code>

Początkowo oczekiwałem, że obsługa zdarzeń w głównej klasie uniemożliwi myślicielowi zbieranie śmieci.

Najwyraźniej tak nie jest.

Zastanawiam się teraz, czy usuwanie śmieci nastąpi niezależnie od tego, czy ten wątek jest „zajęty”, czy nie. Innymi słowy, czy jest szansa, że ​​zostanie zebrane śmieci przed upływem 5 sekundowego limitu czasu?

Innymi słowy, czy zbieracz śmieci może odebrać mojego Myśliciela przed zakończeniem przetwarzania?

questionAnswers(5)

yourAnswerToTheQuestion