Click here to Skip to main content
15,908,661 members
Home / Discussions / C#
   

C#

 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
induur19-Oct-08 23:12
induur19-Oct-08 23:12 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
N a v a n e e t h19-Oct-08 23:29
N a v a n e e t h19-Oct-08 23:29 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
induur19-Oct-08 23:44
induur19-Oct-08 23:44 
GeneralRe: Clearing the invocation list of a static event EventHandler [modified] Pin
N a v a n e e t h20-Oct-08 0:20
N a v a n e e t h20-Oct-08 0:20 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
induur20-Oct-08 0:48
induur20-Oct-08 0:48 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
N a v a n e e t h20-Oct-08 1:08
N a v a n e e t h20-Oct-08 1:08 
GeneralRe: Clearing the invocation list of a static event EventHandler Pin
induur20-Oct-08 1:16
induur20-Oct-08 1:16 
AnswerRe: Clearing the invocation list of a static event EventHandler Pin
N a v a n e e t h21-Oct-08 8:10
N a v a n e e t h21-Oct-08 8:10 
I think your best bet is to create a proxy class for your external library and route all calls through your proxy. Which means, you should not invoke your external library directly. It should be done via this proxy class.

Mimic the event in your proxy class. When items are added, keep the event handlers in a list. Create a method something like ClearInvocationList() in the proxy class, loop through all the event handlers in the list and unregister the event. Clear the list.

You can implement this cleanup in destructor also. But it is not recommended as finalization is performance costly. Look at implementing IDisposable instead.

Sample code follows, (uncompiled and exceptions not handled)
class ExternalLibraryProxy
{
     List<EventHandler> subscriptions = new List<EventHandler>();
     public event EventHandler ThemeChanged
     {
        add
        {
            ExternalLibrary.ThemeChanged += value;
            subscriptions.Add(value);
        }
        remove
        {
            ExternalLibrary.ThemeChanged -= value;
            subscriptions.Remove(value);
        }
     }

     public void ClearInvocationList()
     {
         foreach(EventHandler handler in subscriptions)
             ExternalLibrary.ThemeChanged -= handler;
         subscriptions.Clear();
     }
}


Hope this makes sense.


AnswerRe: Clearing the invocation list of a static event EventHandler Pin
N a v a n e e t h21-Oct-08 8:32
N a v a n e e t h21-Oct-08 8:32 
GeneralRe: Clearing the invocation list of a static event EventHandler [modified] Pin
induur21-Oct-08 21:23
induur21-Oct-08 21:23 
QuestionHow to use a smoothing algorithm? Pin
Abhijit D. Babar19-Oct-08 20:06
Abhijit D. Babar19-Oct-08 20:06 
AnswerRe: How to use a smoothing algorithm? Pin
Christian Graus19-Oct-08 20:41
protectorChristian Graus19-Oct-08 20:41 
AnswerRe: How to use a smoothing algorithm? Pin
Shyam Bharath19-Oct-08 23:38
Shyam Bharath19-Oct-08 23:38 
GeneralRe: How to use a smoothing algorithm? Pin
Abhijit D. Babar20-Oct-08 1:15
Abhijit D. Babar20-Oct-08 1:15 
GeneralRe: How to use a smoothing algorithm? Pin
Pete O'Hanlon20-Oct-08 2:00
mvePete O'Hanlon20-Oct-08 2:00 
QuestionDrag and Drop Controls Pin
User 543536419-Oct-08 18:53
User 543536419-Oct-08 18:53 
AnswerRe: Drag and Drop Controls Pin
N a v a n e e t h19-Oct-08 22:35
N a v a n e e t h19-Oct-08 22:35 
AnswerRe: Drag and Drop Controls Pin
Mycroft Holmes19-Oct-08 22:43
professionalMycroft Holmes19-Oct-08 22:43 
GeneralRe: Drag and Drop Controls Pin
User 543536419-Oct-08 22:50
User 543536419-Oct-08 22:50 
GeneralRe: Drag and Drop Controls Pin
Mycroft Holmes20-Oct-08 0:50
professionalMycroft Holmes20-Oct-08 0:50 
GeneralRe: Drag and Drop Controls Pin
User 543536420-Oct-08 14:09
User 543536420-Oct-08 14:09 
QuestionTCP and ASP and Connections Pin
NULPTR19-Oct-08 11:24
NULPTR19-Oct-08 11:24 
AnswerRe: TCP and ASP and Connections Pin
Tony Richards19-Oct-08 12:17
Tony Richards19-Oct-08 12:17 
GeneralRe: TCP and ASP and Connections Pin
NULPTR19-Oct-08 13:07
NULPTR19-Oct-08 13:07 
QuestionPassing a SQL 2005 vBinary document object (a Word document) to an instance of Word.Document. Pin
dunloe19-Oct-08 9:13
dunloe19-Oct-08 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.