I made a simple download manager based on WebClient. somthing like this:
public class DownloadManager(){
public static Dictionary<string,WebClient> DownloadList{get;set;}
//...
public static WebClient CheckInProgressDownload(string key)
{
System.Net.WebClient result;
if (DownloadsList.TryGetValue(key, out result))
{
return result;
}
else
{
return null;
}
}
}
By getting specific WebClient Object from DownloadList property I can show download progress in a dynamically created Progress Bar. When I Create a new instance of progress bar I have to subscribe to WebClient DownloadFileCompleted and DownloadProgressChanged events.
public calss ProgressConrolGroup(){
public void OnCreate(){
var wc = new DownloadManager.CheckInProgressDownload(stringkey);
if(wc!=null){
wc.DownloadProgressChanged += WC_DownloadProgressChanged;
}
}
private void WC_DownloadProgressChanged(object sender,System.Net.DownloadProgressChangedEventArgs e)
{
progressbar.Progress = e.ProgressPercentage;
}
}
My Problem is as you see , every time I get Existing WebClient Object from list, It has Previously Event handler attached to it. how can I remove its event handlers and add new one?
important note: I saw some answer on stack overflow but none of them were useful to me. like this Get compiler generated delegate for an event and this one remove all EventHandlers of a specific Control. and this example either :How to remove all event handlers from an event .