Esempio n. 1
0
        private static void Example1()
        {
            // first we allocate some objects. the second object we create
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent      = new ObjectWithEvent();
            var objectThatHooksEvent = new ObjectThatHooksEvent(objectWithEvent);

            // we'll set the instance to null and call the GC, but since we
            // have an event still hooked up to the first object, no objects
            // will get finalized yet.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();
            Console.WriteLine("Nothing magical?");

            // let's try unhooking the event now and calling the gc. we should
            // finally be able to get rid of our second object
            Console.WriteLine();
            Console.WriteLine("Press enter and I'll unhook the events for you and call the garbage collector again.");
            Console.ReadLine();
            objectWithEvent.UnhookAll();
            GC.Collect();

            // now that we've ditched the second object, we can safely get rid
            // of the first one!
            Console.WriteLine("Neat-o, eh? Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }
Esempio n. 2
0
        private static void Example1()
        {
            // first we allocate some objects. the second object we create 
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent = new ObjectWithEvent();
            var objectThatHooksEvent = new ObjectThatHooksEvent(objectWithEvent);

            // we'll set the instance to null and call the GC, but since we 
            // have an event still hooked up to the first object, no objects 
            // will get finalized yet.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();
            Console.WriteLine("Nothing magical?");

            // let's try unhooking the event now and calling the gc. we should
            // finally be able to get rid of our second object
            Console.WriteLine();
            Console.WriteLine("Press enter and I'll unhook the events for you and call the garbage collector again.");
            Console.ReadLine();
            objectWithEvent.UnhookAll();
            GC.Collect();

            // now that we've ditched the second object, we can safely get rid
            // of the first one!
            Console.WriteLine("Neat-o, eh? Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }