Exemplo n.º 1
0
        /// <summary>
        /// Execution entry point.
        /// </summary>
        public static void Main()
        {
            // Create an instance of a type that offers an event.
            TypeWithWeakDelegateEvent twwde = new TypeWithWeakDelegateEvent();

            // Create an instance of a type that will register with the event.
            s_someType = new SomeType();

            // Register this object's SomeEventGotRaised method with our the other 
            // object's event.
            twwde.SomeEvent += s_someType.SomeEventGotRaised;

            // Raising the event calls through the delegate.
            twwde.RaiseSomeEvent("Before GC; this appears");

            // As long as a strong reference to the SomeType object exists, it will 
            // not be collected, and every call to RaiseSomeEvent will call the 
            // SomeType object's SomeEventGotRaised method.

            // However, if no strong reference to the SomeType object exists and 
            // garbage collection occurs, future calls to RaiseSomeEvent will NOT 
            // call the SomeType object's SomeEventGotRaised method, because the 
            // object no longer exists in memory.  No exception is raised, because 
            // that is not an error condition.

            // To demonstrate this, set s_someType to null and force garbage 
            // collection to occur.
            s_someType = null;
            Debug.GC(true);

            // Now the SomeType object no longer exists. Raising an event will not 
            // produce output.
            twwde.RaiseSomeEvent("After GC; this doesn't appear");

            Debug.Print("Program ending");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Execution entry point.
        /// </summary>
        public static void Main()
        {
            // Create an instance of a type that offers an event.
            TypeWithWeakDelegateEvent twwde = new TypeWithWeakDelegateEvent();

            // Create an instance of a type that will register with the event.
            s_someType = new SomeType();

            // Register this object's SomeEventGotRaised method with our the other
            // object's event.
            twwde.SomeEvent += s_someType.SomeEventGotRaised;

            // Raising the event calls through the delegate.
            twwde.RaiseSomeEvent("Before GC; this appears");

            // As long as a strong reference to the SomeType object exists, it will
            // not be collected, and every call to RaiseSomeEvent will call the
            // SomeType object's SomeEventGotRaised method.

            // However, if no strong reference to the SomeType object exists and
            // garbage collection occurs, future calls to RaiseSomeEvent will NOT
            // call the SomeType object's SomeEventGotRaised method, because the
            // object no longer exists in memory.  No exception is raised, because
            // that is not an error condition.

            // To demonstrate this, set s_someType to null and force garbage
            // collection to occur.
            s_someType = null;
            Debug.GC(true);

            // Now the SomeType object no longer exists. Raising an event will not
            // produce output.
            twwde.RaiseSomeEvent("After GC; this doesn't appear");

            Debug.Print("Program ending");
        }