如何清除c#中的事件句柄 [英] how to clear event handles in c#

查看:292
本文介绍了如何清除c#中的事件句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用名为client的全局变量



例如

  client.getPagesCompleted + =(s,ee)=> 
{
pages = ee.Result;
BuildPages(tvPages.Items,0);
wait.Close();
};
client.getPagesAsync(cat.MainCategoryID);

我需要清除 getPagesCompleted 的处理程序,设置其他处理程序
如何轻松地清理句柄?$
我知道 client.getPagesCompleted- = new EventHandler(...)。但这是非常困难的。我需要简单的方法。
我使用 client.getPagesCompleted = null 但显示错误。 只使用+ = / - +

解决方案

删除事件处理程序是使用与您通过 + = 添加的处理程序相同的 - = / p>

如果您需要添加和删除处理程序,那么您需要使用匿名方法/委托使用命名方法进行编码。


I'm using global variable named "client"

For example

client.getPagesCompleted += (s, ee) =>
{
    pages = ee.Result;
    BuildPages(tvPages.Items, 0);
    wait.Close();
};
client.getPagesAsync(cat.MainCategoryID);

I need to clear handlers for getPagesCompleted and set another handler.
How to easy clear handles?
I know client.getPagesCompleted-=new EventHandler(...). But it is very difficult. I need easy way. I'm using client.getPagesCompleted=null but error shown. "only use += / -+"

解决方案

The only way to remove an event handler is to use the -= construct with the same handler as you added via +=.

If you need to add and remove the handler then you need to code it in a named method rather using an anonymous method/delegate.

这篇关于如何清除c#中的事件句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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