Click here to Skip to main content
15,313,596 members
Articles / Programming Languages / C#
Posted 9 Oct 2005


103 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.72/5 (21 votes)
9 Oct 20052 min read
Sick and tired of writing diagnostic event handlers to see when a control is firing events? You need EventSpy!

Sample Image - eventspy.jpg


Recently I was writing an application that used two RichTextBoxes next to each other, and I wanted them to scroll simultaneously, but the Scroll events weren't appearing to fire! After 10 minutes of documentation reading I thought, wouldn't it be better if I had a tool that told me when events fired? That's when I started to research EventSpy.

I started off with a bit of reflection, looping through the events of an object and building and linking a System.EventHandler for every event; this worked, but it threw a nasty error if the event wasn't using a System.EventHandler.

foreach (EventInfo info in t.GetEvents())
   info.AddEventHandler(new System.EventHandler(this.EventHandler));

So then I went onto using a completely different method. For each event in the object, EventSpy using Code DOM to spit out a custom event handler code for each event. This allowed me to also capture specialist EventHandlers such as MouseMoveEventHandler. Once custom event handling code has been spat out by Code DOM, EventSpy then compiles the class in memory, and links it up with the object it is spying on. I found this to be a really nifty solution, as it's still very fast but allows me to see all that's going on with the object.

The Code

EventSpy is implemented as a single DLL. Even though it is implemented as a DLL you should only use EventSpy during development. Unfortunately I could not come up with a good enough way to have EventSpy as a standalone exe, so I did the next best thing, two lines of code. Here is some sample code on how to use EventSpy, this just spies on a very basic class that throws an event. EventSpy can be used on any object, such as a textbox, a button or a custom business class.

public class Dummy
    public delegate void DummyEventHandler(string something);
    public event DummyEventHandler DummyEvent;

    public void RaiseEvent()
        if (DummyEvent != null)
            DummyEvent("Hello World!");

EventSpy.EventListener listener = new EventSpy.EventListener();
Dummy dummy = new Dummy();



After the listener.AttachTo line, the main EventSpy window will pop up, with a list of events that have occurred, and you may filter out events that you don't want to spy on, or view what was passed in the events parameters. Very cool if you ask me, it's saved me loads of time on Windows Forms development.


You may use EventSpy however you like, just give me a bit of credit. If people are interested in helping improve EventSpy, then drop me a line and we could get a SourceForge project going. EventSpy definitely needs a code cleanup, I'll get that done ASAP, I kind of rushed EventSpy so that I could get using it!


  • 2005-09-10 - Original article posted.


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

Martin Carolan
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionCan it trace event from C++ app control? Pin
udomnoke21-Aug-08 5:53
Memberudomnoke21-Aug-08 5:53 
GeneralLoad the eventSpy in Debug with Late binding Pin
SalizarMarxx4-Jun-07 19:17
MemberSalizarMarxx4-Jun-07 19:17 

if (System.IO.File.Exists(System.IO.Path.Combine(apps.GetSettingByKey(ApplicationSettings.Tags.ApplicationPath), "EventSpy.dll")))
object[] param = new object[] { dataGridView1 };
Assembly asm = Assembly.LoadFile(System.IO.Path.Combine(apps.GetSettingByKey(ApplicationSettings.Tags.ApplicationPath), "EventSpy.dll"));
Type type = asm.GetType("EventSpy.EventListener");
object typeeventspy = System.Activator.CreateInstance(type);
type.InvokeMember("AttachTo", BindingFlags.InvokeMethod, null, typeeventspy, param);
catch (Exception) { }
GeneralOther implementation available Pin
DenisK17-Oct-05 22:17
professionalDenisK17-Oct-05 22:17 
GeneralDebug one application from another one Pin
canozurdo15-Oct-05 5:44
Membercanozurdo15-Oct-05 5:44 
GeneralRe: Debug one application from another one Pin
Harm Salomons23-Oct-05 9:55
MemberHarm Salomons23-Oct-05 9:55 
Questionsomething missing? Pin
Harm Salomons14-Oct-05 11:21
MemberHarm Salomons14-Oct-05 11:21 
AnswerRe: something missing? Pin
Anonymous14-Oct-05 12:55
MemberAnonymous14-Oct-05 12:55 
QuestionI am interested in your project. Pin
Wooseok Seo9-Oct-05 23:56
MemberWooseok Seo9-Oct-05 23:56 
AnswerRe: I am interested in your project. Pin
Anonymous10-Oct-05 7:47
MemberAnonymous10-Oct-05 7:47 
GeneralHappy birthday! Pin
Xirius10-Oct-05 20:35
MemberXirius10-Oct-05 20:35 
GeneralRe: Happy birthday! Pin
Martin Carolan11-Oct-05 5:54
MemberMartin Carolan11-Oct-05 5:54 
GeneralThe idea is awesome Pin
Robert Rohde9-Oct-05 11:40
MemberRobert Rohde9-Oct-05 11:40 
GeneralRe: The idea is awesome Pin
Martin Carolan9-Oct-05 11:42
MemberMartin Carolan9-Oct-05 11:42 
GeneralThank you Pin
Martin Carolan9-Oct-05 10:39
MemberMartin Carolan9-Oct-05 10:39 
GeneralRe: Thank you Pin
KiwiPiet9-Oct-05 16:03
MemberKiwiPiet9-Oct-05 16:03 

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.