Archive

Archive for the ‘GC’ Category

When and how to use GC.KeepAlive in .Net

August 27th, 2011 1 comment

Let’s look on the following C# code example (ignore the correctness of the code and its logic)

    /// <summary>
    /// Indicates any service Logger for example
    /// </summary>
    class MyService
    {
        public MyService(string fileName)
        {
            //Do some initializations
        }

        public static void DoSomething()
        {
            //some calculations, or logs creation
        }
    }
    //*************************************************
    /// <summary>
    /// Usage example class 
    /// </summary>
    class MyLogic
    {
        public void Foo()
        {
            //cals static method of the initialized service
            MyService.DoSomething();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Create our service 
            MyService myService = new MyService("temp.txt");
            //Wait here for user
            Console.ReadLine();
        }
    }

Read more…

Categories: .Net, GC Tags: ,