• WeakEventHandler: Avoiding GC Problem by Strong Reference of an Event


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestWeakReference
    {
        
    class WeakEventHandler<T> : WeakReference where T : EventArgs
        {
            
    public WeakEventHandler() : base(null) { }

            
    public WeakEventHandler(EventHandler<T> handler) : base(handler) { }

            
    public WeakEventHandler(EventHandler<T> target, bool trackResurrection) : base(target, trackResurrection) { }

            
    public new EventHandler<T> Target
            {
                
    get
                {
                    
    return (EventHandler<T>)base.Target;
                }
                
    set
                {
                    
    base.Target = value;
                }
            }

            
    public static explicit operator EventHandler<T>(WeakEventHandler<T> weakEventHandler)
            {
                
    return weakEventHandler.Target;
            }
        }

        
    class EventHost
        {
            
    private WeakEventHandler<EventArgs> _event = new WeakEventHandler<EventArgs>();

            
    public event EventHandler<EventArgs> Event
            {
                add { _event.Target 
    += value; }
                remove { _event.Target 
    -= value; }
            }

            
    public void FireEvent()
            {
                
    if (_event.Target != null)
                {
                    _event.Target(
    this, EventArgs.Empty);
                }
            }
        }

        
    class EventClient
        {
            
    public void SayHello(object obj, EventArgs args)
            {
                Console.WriteLine(
    "Hello~");
            }

            
    ~EventClient()
            {
                Console.WriteLine(
    "Client Dies!");
            }
        }

        
    class Program
        {
            
    static void Main(string[] args)
            {
                EventHost host 
    = new EventHost();
                EventClient client 
    = new EventClient();
                host.Event 
    += client.SayHello;
                host.FireEvent(); 
    // Client will say "Hello~";

                client 
    = null;
                GC.Collect(); 
    // Client dies here!

                host.FireEvent(); 
    // oops, nothing happen
            }
        }
    }


    More about this:
    1. Using Weak References
    2. Simulating “Weak Delegates” in the CLR
    3. An Easy to Use Weak Referenced Event Handler Factory for .NET 2.0

  • 相关阅读:
    【Python3网络爬虫开发实战】 1-开发环境配置
    Elasticsearch 基本介绍及其与 Python 的对接实现
    深度学习 GPU环境 Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.
    React组件方法中为什么要绑定this
    中级前端开发推荐书籍
    20万行代码,搞得定不?
    华为云数据库TaurusDB性能挑战赛,50万奖金等你来拿!
    00036_private
    使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
    http304状态码缓存设置问题
  • 原文地址:https://www.cnblogs.com/Dah/p/671141.html
Copyright © 2020-2023  润新知