取消订阅事件 [英] Unsubscribing from an event

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

问题描述

我有以下功能.

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

问题是:我如何取消订阅?或者更重要的是,我需要吗?

由于我将在表单本身的表单的 Load 事件上使用这个,如果表单关闭,我真的需要取消订阅吗?

(经过一些简单的阅读和对 GC 的一些了解后,我怀疑我不需要取消订阅,但我不确定)

//一个使用函数的例子private void Form1_Load(对象发送者,EventArgs e){MyEventHandler.CreateKeyDownEventHandlers(this);}//功能公共静态无效CreateEventHandlers(控制Ctrl){foreach(Ctrl.Controls 中的 Control c){//bool Rules(Control) 一个确定控制什么的函数//应用处理程序的事件如果(规则(c)){c.KeyDown += (s, e) =>{//做点什么};}//一个控件可能是一个分组框,所以我们希望它们包含//控制也if (c.Controls != null){if (c.Controls.Count > 0){CreateEventHandlers(c);}}}}

这是我第一次尝试使用事件、委托、匿名函数和 lambda,所以如果我做了一些非常愚蠢的事情,请告诉我.

解决方案

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

是否需要取消订阅:考虑对象的生命周期.您在静态方法中创建匿名函数并将 附加到我假设您控制生命周期的控件.

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

如果情况反过来,在静态方法中创建的东西引用了控件,就像在静态上下文中将控件委托添加到事件中一样,那么 GC 无法处理控件,直到引用删除了它们,如果在静态方法中完成,则不会发生这种情况.

I have the following function.

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?

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?

(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);
                }
            }

        }
    }

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.

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.

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天全站免登陆