0

我正在使用这段代码来注册事件并希望在完成它的任务后取消注册事件但不知道该怎么做问题是我正在使用本地对象来注册事件..

代码..

public void loadData()
{
//Here client is loacal object..
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}
void AccessTokenDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
}
4

2 回答 2

0
Maybe you can try this:
public void loadData()
{
//Here client is loacal object..
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}
void AccessTokenDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    Client client = sender as Client;
    if(client != null) 
        client.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}
于 2013-05-14T01:56:38.643 回答
0

如果我理解正确,您希望在下载完成后删除您的事件处理程序。要删除事件处理程序,您需要做的就是:

client.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);

请注意,-=而不是+=.

将此代码放在下载完成的地方,你应该没问题。

于 2013-04-03T13:22:46.533 回答