取消订阅活动 [英] Unsubscribing from an event

查看:77
本文介绍了取消订阅活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能.

它的作用是,给定一个控件(最有可能是Windows窗体),我想让所有遵守"规则的控件包含(一个筛选我想要的控件的函数)进行订阅到一个事件(比如说KeyDown).

What it does is, given a Control (most likely a windows form) i want to have all of the controls contained that "obey" the Rules ( a function screening the controls i want ) subscribe to an event (lets say KeyDown).

问题是:我该如何退订?或者更重要的是,我需要吗?

The question is: how do i unsubscribe? Or more importantly, do i need to?

由于我将在窗体本身的窗体的Load事件上使用此控件,所以如果窗体关闭,我真的需要取消订阅吗?

Since i will be using this one on the Load event of forms on the form itself will i really need to unsubscribe if the form closes?

(经过一番阅读和对GC的一点了解之后,我怀疑我不需要退订,但我不确定)

(after some light reading and a little understanding of the GC i suspect i don't need to unsubscribe but I'm not sure)

//an example of using the function
    private void Form1_Load(object sender, EventArgs e)
    {
        MyEventHandler.CreateKeyDownEventHandlers(this);
    }

//the function
    public static void CreateEventHandlers(Control Ctrl)
    {
        foreach (Control c in Ctrl.Controls)
        {
            //bool Rules(Control) a function that determines to what controls'
            //events to apply the handler 
            if ( Rules(c) )
            {
                c.KeyDown += (s, e) =>
                {
                  // do something
                };

            }

            //a control might be a groupbox so we want their contained
            //controls also
            if (c.Controls != null)
            {
                if (c.Controls.Count > 0)
                {
                    CreateEventHandlers(c);
                }
            }

        }
    }

这是我第一次尝试使用事件,委托,匿名函数和lambda,因此,如果我做的事情确实很愚蠢,请告诉我.

This is my first try with events, delegates, anonymous functions and lambdas so if i did something really stupid tell me.

推荐答案

首先,我认为除非将匿名函数分配给处理程序变量并将该变量添加到事件中然后从事件中删除,否则您不能取消订阅该匿名函数.

First, I think you cannot unsubscribe an anonymous function unless it's assigned to a handler variable and that variable is addded to and then removed from the event.

是否需要退订:考虑对象的生存期.您可以通过静态方法创建匿名函数,并将附加到我认为您可以控制生命周期的控件上.

Whether you need to unsubscribe: Think about the object lifetimes. You create anonymous functions in a static method and attach the to controls of which I assume you control the lifetimes.

当您处置这些控件之一时,它们将不再引用匿名函数,并且GC可以杀死它们(匿名函数),因此您不需要退订.

When you dispose one of these controls, they will no longer be referencing the anonymous functions and the GC can kill them (the anonymous functions), so you don't need to unsubscribe.

如果情况发生了逆转,并且在静态方法中创建的内容引用了控件,就像在静态上下文中将控件委托添加到事件中一样,则在引用之前,GC无法处理控件删除它们,如果以静态方法完成,则不会发生.

If the situation was reversed and something that was created in the static method referenced the controls, as if a control delegate was added to an event in the static context, then the GC couldn't take care of the controls until the reference to them was removed, which wouldn't happen if it was done in the static method.

这篇关于取消订阅活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆