我想实现一个负责的方法来订阅我的Webclient到处理程序,当我想取消订阅它似乎没有正确完成。 我有一个例子:
我的函数用于订阅
private void SendRequest(Action<object, UploadStringCompletedEventArgs> callback, string url)
{
if (!wClient.IsBusy)
{
wClient.UploadStringCompleted += new UploadStringCompletedEventHandler(callback);
wClient.UploadStringAsync(new Uri(url), "POST");
[...]
}
}
我的处理程序
private void wClient_request1Completed(object sender, UploadStringCompletedEventArgs e)
{
wClient.UploadStringCompleted -= wClient_request1Completed;
[...]
}
private void wClient_request2Completed(object sender, UploadStringCompletedEventArgs e)
{
wClient.UploadStringCompleted -= wClient_request2Completed;
[...]
}
我使用这样的方法
private WebClient wClient = new WebClient();
SendRequest(wClient_request1Completed, myUrl1);
// wClient_request1Completed(..) is run successfully
[... Request 1 is already completed ...]
SendRequest(wClient_request2Completed, myUrl2);
// wClient_request1Completed(..) and wClient_request2Completed(..) are run
你对我的问题有所了解吗? 非常感谢你!
答案 0 :(得分:0)
那是因为您隐式创建了一个新的委托作为SendRequest
方法的参数。基本上,您当前的代码可以重写为:
// Done implicitly by the compiler
var handler = new Action<object, UploadStringCompletedEventArgs>(wClient_request1Completed);
wClient.UploadStringCompleted += handler;
// ...
wClient.UploadStringCompleted -= wClient_request1Completed // (instead of handler)
修复它的一种方法可能是使用UploadStringAsync
方法的payload参数来保持对处理程序的引用:
var handler = new UploadStringCompletedEventHandler(callback);
wClient.UploadStringCompleted += handler;
wClient.UploadStringAsync(new Uri(url), "POST", null, handler);
然后,在UploadStringCompleted
事件中:
private void wClient_request1Completed(object sender, UploadStringCompletedEventArgs e)
{
var handler = (UploadStringCompletedEventHandler)e.UserState;
wClient.UploadStringCompleted -= handler ;
[...]
}
也就是说,您应该考虑切换到HttpClient
和async / await编程模型,因为它会使您的代码更容易理解。