{ page...">

How to clear event handlers in C #

I am using a global variable called "client"

for instance

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

I need to clear the handlers for getPagesCompletedand install another handler.
How easy is it to clean the pens?
I know client.getPagesCompleted-=new EventHandler(...). But it is so hard. I need an easy way. I use client.getPagesCompleted=null, but an error is displayed."only use += / -+"

+1
source share
3 answers

The only way to remove the event handler is to use the construct -=with the same handler that you added through +=.

If you need to add and remove a handler, you need to encode it using the named method using the anonymous method / delegate.

+6

; -, -. - :

MyEventHandler handler = (s, ee) => 
{ 
    pages = ee.Result; 
    BuildPages(tvPages.Items, 0); 
    wait.Close(); 
}; 

client.getPagesCompleted += handler; // Add event handler
// ...
client.getPagesCompleted -= handler; // Remove event handler
+2

Save the event object in a variable and use -=to unsubscribe.

0
source

Source: https://habr.com/ru/post/1750911/


All Articles