Events: Custom Add and Remove with Global delegate cache. : event « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » event 
23.64.9.Events: Custom Add and Remove with Global delegate cache.
// copyright 2000 Eric Gunnerson
// From A Programmer's Introduction to C# 2.0, Third Edition

using System;
using System.Collections;
using System.Runtime.CompilerServices;

//
// Global delegate cache. Uses a two-level hashtable. The delegateStore hashtable 
// stores a hashtable keyed on the object instance, and the instance hashtable is 
// keyed on the unique key. This allows fast tear-down of the object when it's destroyed.
//
public class DelegateCache
{
    private DelegateCache() {}    // nobody can create one of these
    
    Hashtable delegateStore = new Hashtable();    // top level hash table
    
    static DelegateCache dc = new DelegateCache();    // our single instance
    
    Hashtable GetInstanceHash(object instance)
    {
        Hashtable instanceHash = (HashtabledelegateStore[instance];
        
        if (instanceHash == null)
        {
            instanceHash = new Hashtable();
            delegateStore[instance= instanceHash;
            
        }
        return(instanceHash);
    }
    
    public static void Combine(Delegate myDelegate, object instance, object key)
    {
        lock(instance)
        {
            Hashtable instanceHash = dc.GetInstanceHash(instance);        
            
            instanceHash[key= Delegate.Combine((DelegateinstanceHash[key],
            myDelegate);
        }
    }
    
    public static void Remove(Delegate myDelegate, object instance, object key)
    {
        lock(instance)
        {
            Hashtable instanceHash = dc.GetInstanceHash(instance);        
            
            instanceHash[key= Delegate.Remove((DelegateinstanceHash[key],
            myDelegate);
        }
    }
    
    public static Delegate Fetch(object instance, object key)
    {
        Hashtable instanceHash = dc.GetInstanceHash(instance);        
        
        return((DelegateinstanceHash[key]);
    }
    
    public static void ClearDelegates(object instance)
    {
        dc.delegateStore.Remove(instance);
    }
}

public class Button
{
    public void TearDown()
    {
        DelegateCache.ClearDelegates(this);
    }
    
    
    public delegate void ClickHandler(object sender, EventArgs e);
    
    static object clickEventKey = new object();
    
    public event ClickHandler Click
    {
        add
        {
            DelegateCache.Combine(value, this, clickEventKey);
        }
        
        remove
        {
            DelegateCache.Remove(value, this, clickEventKey);
        }
    }
    
    protected void OnClick()
    {
        ClickHandler ch = (ClickHandlerDelegateCache.Fetch(this, clickEventKey);
        
        if (ch != null)
        ch(this, null);
    }
    
    public void SimulateClick()
    {
        OnClick();
    }
}

class Test
{
    static public void ButtonHandler(object sender, EventArgs e)
    {
        Console.WriteLine("Button clicked");
    }
    
    public static void Main()
    {
        Button button = new Button();
        
        button.Click += new Button.ClickHandler(ButtonHandler);
        
        button.SimulateClick();
        
        button.Click -= new Button.ClickHandler(ButtonHandler);
        
        button.TearDown();
    }
}
Button clicked
23.64.event
23.64.1.Events
23.64.2.Using Event Accessors
23.64.3.Event and event handler
23.64.4.Use delegate to handle events
23.64.5.Fire event in property setter
23.64.6.Events: add and remove functions for a private delegate event
23.64.7.Events: add and remove functions
23.64.8.Events add and remove with synchronized block
23.64.9.Events: Custom Add and Remove with Global delegate cache.
23.64.10.A .NET-compatible event
23.64.11.Use the built-in EventHandler delegate
23.64.12.Use an anonymous method as an event handler
23.64.13.Ignored Parameters Anonymous Methods for Button click, kepressed and mouse clicked action
23.64.14.Events and form controls
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.