Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't GC figure it out?

My question is simple, why GC can't figure it out that timer object in the main should be garbage collected along with the timer inside TestTimer and associated EventHandler?

Why am I continously getting console.Writeline output?

class Program
{
    public static void Main()
    {       
       TestTimer timer = new  TestTimer();
       timer = null;
       GC.Collect();
       GC.WaitForPendingFinalizers();
       Console.ReadKey();
    }
}

public class TestTimer
{
    private Timer timer;

    public TestTimer()
    {
        timer = new Timer(1000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Start();
    }

    private void timer_Elapsed(Object sender, ElapsedEventArgs args)
    {
        Console.Write("\n" + DateTime.Now);
    }
}
like image 544
Haris Hasan Avatar asked Jul 23 '11 10:07

Haris Hasan


People also ask

Which compounds Cannot be detected by GC?

Following compounds cannot be detected: Compounds which are too volatile: most C1-C4 aliphatic hydrocarbons, methanol, methanal. Some important (odorous) inorganic compounds like hydrogen sulphide and ammonia. To analyse these compounds a specific analytical method is required (GC or colorimetric methods)

How do you identify a GC compound?

A GC/MS system scans the samples continuously throughout the separation and, ultimately, the chromatogram shows the retention times. The chromatogram peak is analyzed by the mass spectrometer which leads to the identification of the separated components.

Why is GC not used for all Analyses?

Utilisation of GC creates a high risk of thermal degradation of vitamins, even when derivatisation is done prior to GC separation. An example of this is GC-based vitamin K applications, which, due to high retention times and possible on-column degradation, are not very well suited for GC analysis.

What are the limitations of gas chromatography?

The principal limitation of gas chromatographic application is given by an insufficient volatility of the species to be separated. Elevated temperatures have to be applied if the application range is to be extended and to achieve steep peak profiles, i.e. low detection limits at high resolution.


Video Answer


1 Answers

Don't depend on the GC, use the Dispose pattern to properly dispose the TestTimer (which then should dispose the Timer).

However, what happens is that the timer keeps itself alive by getting a GC handle on itself. Read this blog post:

http://nitoprograms.blogspot.com/2011/07/systemthreadingtimer-constructor-and.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FOlZtT+%28Nito+Programming%29

like image 121
Lucero Avatar answered Oct 07 '22 15:10

Lucero