13

更新

我已将此处的各种答案组合成一个新问题的“明确”答案。

原始问题

在我的代码中,我有一个事件发布者,它存在于应用程序的整个生命周期(这里简化为基本要素):

public class Publisher
{
    //ValueEventArgs<T> inherits from EventArgs
    public event EventHandler<ValueEventArgs<bool>> EnabledChanged; 
}

因为这个发布者可以在任何地方使用,我很高兴自己创建了这个小助手类,以避免在所有订阅者中重写处理代码:

public static class Linker
{
    public static void Link(Publisher publisher, Control subscriber)
    {
         publisher.EnabledChanged += (s, e) => subscriber.Enabled = e.Value;
    }

    //(Non-lambda version, if you're not comfortable with lambdas)
    public static void Link(Publisher publisher, Control subscriber)
    {
         publisher.EnabledChanged +=
             delegate(object sender, ValueEventArgs<bool> e)
             {
                  subscriber.Enabled = e.Value;
             };
    }
}

它工作得很好,直到我们开始在较小的机器上使用它,当我开始偶尔:

System.ComponentModel.Win32Exception
Not enough storage is available to process this command

事实证明,代码中有一个地方是订阅者控件被动态创建、添加和从表单中删除的。鉴于我对垃圾收集等的深入了解(即直到昨天还没有),我从来没有想过要在我身后进行清理,因为在绝大多数情况下,订阅者也会在应用程序的整个生命周期内都存在。

我已经用Dustin Campbell 的 WeakEventHandler摆弄了一段时间,但它不适用于匿名代表(无论如何都不适合我)。

反正有这个问题吗?我真的很想避免在整个商店复制粘贴样板代码。

(哦,别问我为什么我们一直在创建和销毁控件,这不是我的设计决定......)

(PS:这是一个winforms应用程序,但我们已经升级到VS2008和.Net 3.5,我应该考虑使用弱事件模式吗?)

(PPS:来自 Rory 的好答案,但如果有人能想出一个与 WeakEventHandler 等效的方法,这样我就不必记住显式 UnLink/Dispose,那会很酷......)

编辑我必须承认我通过“回收”有问题的控件来解决这个问题。然而,解决方法又回来困扰我,因为我使用的“钥匙”显然不是唯一的(呜咽)。我刚刚在这里发现了其他链接(尝试过这个 - 似乎有点太弱了- 即使目标仍然存在,GC也会清除代表,下面的 s,oɔɯǝɹ 答案也有同样的问题),这里(强制你修改发布者,并且不't 真正与匿名代表一起工作)和这里(引用为不完整的达斯汀坎贝尔)。

我突然想到,我正在寻找的东西在语义上可能是不可能的——闭包的设计目的是“即使在我离开后也能闲逛”。

我找到了另一种解决方法,所以我会坚持下去,等待众神的声音

4

4 回答 4

5

我知道这个问题很古老,但是地狱 - 我找到了它,而且我认为其他人也可能这样做。我正在尝试解决相关问题,并且可能会有一些见解。

您提到了 Dustin Campbell 的 WeakEventHandler - 它确实不能通过设计使用匿名方法。当我意识到 a) 在 99% 的情况下我需要这样的东西时,我试图将一些东西摆弄在一起,他原来的解决方案会更安全,并且 b) 在我必须这样做的少数情况下(注意:有to,而不是“想要,因为 lambdas 更漂亮和简洁”)如果你有点聪明,它可以让它工作。

您的示例似乎完全是一种一次性的情况,其中有点棘手可能会导致一个相当简洁的解决方案。


public static class Linker {
    public static void Link(Publisher publisher, Control subscriber) {
        // anonymous method references the subscriber only through weak 
        // references,so its existance doesn't interfere with garbage collection
        var subscriber_weak_ref = new WeakReference(subscriber);

        // this instance variable will stay in memory as long as the  anonymous
        // method holds a reference to it we declare and initialize  it to 
        // reserve the memory (also,  compiler complains about uninitialized
        // variable otherwise)
        EventHandler<ValueEventArgs<bool>> handler = null;

        // when the handler is created it will grab references to the  local 
        // variables used within, keeping them in memory after the function 
        // scope ends
        handler = delegate(object sender, ValueEventArgs<bool> e) {
            var subscriber_strong_ref = subscriber_weak_ref.Target as Control;

            if (subscriber_strong_ref != null) 
                subscriber_strong_ref.Enabled = e.Value;
            else {
                // unsubscribing the delegate from within itself is risky, but
                // because only one instance exists and nobody else has a
                // reference to it we can do this
                ((Publisher)sender).EnabledChanged -= handler;

                // by assigning the original instance variable pointer to null
                // we make sure that nothing else references the anonymous
                // method and it can be collected. After this, the weak
                //  reference and the handler pointer itselfwill be eligible for
                // collection as well.
                handler = null; 
            }
        };

        publisher.EnabledChanged += handler;
    }
}

据传 WPF 弱事件模式会带来很多开销,因此在这种特殊情况下我不会使用它。此外,在 WinForm 应用程序中引用核心 WPF 库似乎也有点繁重。

于 2009-09-19T03:17:05.033 回答
4

如果您保留对匿名委托的引用,然后在从表单中删除控件时将其删除,则应该允许对控件和匿名委托进行垃圾收集。

所以是这样的:

public static class Linker
{

    //(Non-lambda version, I'm not comfortable with lambdas:)
    public static EventHandler<ValueEventArgs<bool>> Link(Publisher publisher, Control subscriber)
    {
         EventHandler<ValueEventArgs<bool>> handler = delegate(object sender, ValueEventArgs<bool> e)
             {
                  subscriber.Enabled = e.Value;
             };
         publisher.EnabledChanged += handler;
         return handler;
    }

    public static void UnLink(Publisher publisher, EventHandler<ValueEventArgs<bool>> handler)
    {
        publisher.EnabledChanged -= handler;
    }

}

有关删除委托的示例,请参阅C# 中的取消订阅匿名方法。

于 2008-12-16T12:26:15.797 回答
1

我最近基于 WeakReference 制作的一些示例代码:

// strongly typed weak reference
public class WeakReference<T> : WeakReference
    where T : class
{
    public WeakReference(T target)
        : base(target)
    { }

    public WeakReference(T target, bool trackResurrection)
        : base(target, trackResurrection)
    { }

    public new T Target
    {
        get { return base.Target as T; }
        set { base.Target = value; }
    }
}

// weak referenced generic event handler
public class WeakEventHandler<TEventArgs> : WeakReference<EventHandler<TEventArgs>>
    where TEventArgs : EventArgs
{
    public WeakEventHandler(EventHandler<TEventArgs> target)
        : base(target)
    { }

    protected void Invoke(object sender, TEventArgs e)
    {
        if (Target != null)
        {
            Target(sender, e);
        }
    }

    public static implicit operator EventHandler<TEventArgs>(WeakEventHandler<TEventArgs> weakEventHandler)
    {
        if (weakEventHandler != null)
        {
            if (weakEventHandler.IsAlive)
            {
                return weakEventHandler.Invoke;
            }
        }

        return null;
    }
}

// weak reference common event handler
public class WeakEventHandler : WeakReference<EventHandler>
{
    public WeakEventHandler(EventHandler target)
        : base(target)
    { }

    protected void Invoke(object sender, EventArgs e)
    {
        if (Target != null)
        {
            Target(sender, e);
        }
    }

    public static implicit operator EventHandler(WeakEventHandler weakEventHandler)
    {
        if (weakEventHandler != null)
        {
            if (weakEventHandler.IsAlive)
            {
                return weakEventHandler.Invoke;
            }
        }

        return null;
    }
}

// observable class, fires events
public class Observable
{
    public Observable() { Console.WriteLine("new Observable()"); }
    ~Observable() { Console.WriteLine("~Observable()"); }

    public event EventHandler OnChange;

    protected virtual void DoOnChange()
    {
        EventHandler handler = OnChange;

        if (handler != null)
        {
            Console.WriteLine("DoOnChange()");
            handler(this, EventArgs.Empty);
        }
    }

    public void Change()
    {
        DoOnChange();
    }
}

// observer, event listener
public class Observer
{
    public Observer() { Console.WriteLine("new Observer()"); }
    ~Observer() { Console.WriteLine("~Observer()"); }

    public void OnChange(object sender, EventArgs e)
    {
        Console.WriteLine("-> Observer.OnChange({0}, {1})", sender, e);
    }
}

// sample usage and test code
public static class Program
{
    static void Main()
    {
        Observable subject = new Observable();
        Observer watcher = new Observer();

        Console.WriteLine("subscribe new WeakEventHandler()\n");
        subject.OnChange += new WeakEventHandler(watcher.OnChange);
        subject.Change();

        Console.WriteLine("\nObserver = null, GC");
        watcher = null;
        GC.Collect(0, GCCollectionMode.Forced);
        GC.WaitForPendingFinalizers();

        subject.Change();

        if (Debugger.IsAttached)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}

生成以下输出:

new Observable()
new Observer()
subscribe new WeakEventHandler()

DoOnChange()
-> Observer.OnChange(ConsoleApplication4.Observable, System.EventArgs)

Observer = null, GC
~Observer()
DoOnChange()
~Observable()
Press any key to continue . . .

(请注意,取消订阅 (-=) 不起作用)

于 2009-06-05T13:45:20.800 回答
0

进一步基于Egor 的回答,我想尝试构建一个版本,我不必提前确定要附加到哪个事件。

我只设法使它与通用事件处理程序一起工作:对于“标准”事件处理程序(例如 FormClosingEventHandler),这有点棘手,因为你不能有类型约束where T : delegate(除非你的名字以Pony结尾)。

private static void SetAnyGenericHandler<S, T>(
     Action<EventHandler<T>> add,     //to add event listener to publisher
     Action<EventHandler<T>> remove,  //to remove event listener from publisher
     S subscriber,                    //ref to subscriber (to pass to consume)
     Action<S, T> consume)            //called when event is raised*
         where T : EventArgs 
         where S : class
{
    var subscriber_weak_ref = new WeakReference(subscriber);
    EventHandler<T> handler = null;
    handler = delegate(object sender, T e)
    {
        var subscriber_strong_ref = subscriber_weak_ref.Target as S;
        if(subscriber_strong_ref != null)
        {
            Console.WriteLine("New event received by subscriber");
            consume(subscriber_strong_ref, e);
        }
        else
        {
            remove(handler);
            handler = null;
        }
    };
    add(handler);
}

(*我确实EventHandler<T> consume在这里尝试过,但是调用代码变得很难看,因为您必须在消费 lambda 中将 s 强制转换为订阅者。)

调用代码示例,取自上面的示例:

SetAnyGenericHandler(
    h => publisher.EnabledChanged += h, 
    h => publisher.EnabledChanged -= h, 
    subscriber, 
    (Subscriber s, ValueEventArgs<bool> e) => s.Enabled = e.Value);

或者,如果您愿意

SetAnyGenericHandler<Subscriber, ValueEventArgs<bool>>(
    h => publisher.EnabledChanged += h, 
    h => publisher.EnabledChanged -= h, 
    subscriber, 
    (s, e) => s.Enabled = e.Value);

能够仅将事件作为一个参数传递会很好,但是您无法从事件中访问添加/删除,就像您无法从属性访问 get/set 一样(我认为无需进行令人讨厌的反射操作) )。

于 2009-11-06T12:26:03.830 回答