Click here to Skip to main content
Licence 
First Posted 11 Apr 2007
Views 13,931
Downloads 119
Bookmarked 7 times

Custom Event Handlers

By | 11 Apr 2007 | Article
Improved custom implementation of event handling mechanism in c#

Introduction

I found it useful to use custom EventHandlers instead of framework's EventHandler and EventHandler<T> delegates.

  1. Often I had a mistake of multiple assigns of same method to delegate what caused problems.
  2. Calling UI thread from different thread is forbidden and problematic in framework and I found on internet nice solution that should be part of Event class for consistency.
  3. I'd like to have nicer event calling mechanism without checking for null .


Solution : class Event.cs .

  • Uses standard framework delegates EventHandler and EventHandler<T> so it can be easy used in interfaces interchangeable with standard events.
  • Exception when duplicate method assign.
  • Automatic using of ISynchronizeInvoke when calling UI thread.
  • Uses standard framework EventArgs ;
  • Serialization support (no attributes to bypass serialization needed)
  • Event class contains also static methods for firing EventHandlers with support of ISynchronizeInvoke for UI firing from different thread. Syntax: Event.Fire(...); Event.SafeFire(...);

Using the code

-Event myEvent = new Event() (uses EventHandler internally) or Event<T> myEvent = new Event<T>() (uses EventHandler<T>)
-assigning via standard += -=
-myEvent.Fire(senderObject) , myEvent.Fire(args,senderObject) and for safe UI firing support myEvent.SafeFire(...
- for usage in interfaces use in class:
       public event EventHandler MyEvent
        {
            add { myEvent+= value; }
            remove { myEvent -= value; }
        }   

 and in interface then standard : event EventHandler MyEvent; 

History

9 May 2007 - Initial release on Code Project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rene Dohan



Italy Italy

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralmyEvent.SafeFire problem Pinmembertvproducer33:32 1 Aug '07  
GeneralUpdated Sample PinmemberSteveC-A98:08 9 Jul '07  
GeneralRe: Updated Sample PinmemberRene Dohan11:09 9 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 11 Apr 2007
Article Copyright 2007 by Rene Dohan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid