Monitoring all the events

N

Nash

Dear All,
I am beginner in C#. I am writing an application which will monitor
for all the events that are generated in my application and log it in
a file. Is there any way to put a hook on message queue and achieve
that or is there by any means i can subscribe my method to respond to
all the events and then calling appropriate functions?

Thanks in advance.
 
A

Alberto Poblacion

Nash said:
I am beginner in C#. I am writing an application which will monitor
for all the events that are generated in my application and log it in
a file. Is there any way to put a hook on message queue and achieve
that or is there by any means i can subscribe my method to respond to
all the events and then calling appropriate functions?

You can hook the message queue with
System.Windows.Forms.Application.AddMessageFilter(new MyFilter());
Where MyFilter should implement IMessageFilter, which has a method
"PreFilterMessage" that will give you the windows messages. Note that there
is a huge number of messages going through this, so if you try to log them
all you are probably going to be overflowed.

If you want to capture all the Events raised by the Form object, you can
just double-click on each of them in the events window in the Designer. This
will create a method for each of the events in your form.cs. You can then
copy and paste a call to your logging routine inside each of these methods.
This is not very pretty, but is much easier to do than trying to use
Reflection to enumerate all of the events and hooking them on-the-fly.
 
N

Nash

   You can hook the message queue with
   System.Windows.Forms.Application.AddMessageFilter(new MyFilter());
   Where MyFilter should implement IMessageFilter, which has a method
"PreFilterMessage" that will give you the windows messages. Note that there
is a huge number of messages going through this, so if you try to log them
all you are probably going to be overflowed.

   If you want to capture all the Events raised by the Form object, you can
just double-click on each of them in the events window in the Designer. This
will create a method for each of the events in your form.cs. You can then
copy and paste a call to your logging routine inside each of these methods..
This is not very pretty, but is much easier to do than trying to use
Reflection to enumerate all of the events and hooking them on-the-fly.

Dear Alberto,
Thanks for your response. your first approach seems intresting
because later if i add another control to the form i dont need to put
my logging routine inside that event handler. I am right now intrested
in loging Button Click only. But i have one question suppose if i am
having a button and its event handler in the class and i am doing the
hook also. Will PreFilterMessage and MyEventHanlder will get called?
 
A

Alberto Poblacion

Nash said:
[...] I am right now intrested
in loging Button Click only. But i have one question suppose if i am
having a button and its event handler in the class and i am doing the
hook also. Will PreFilterMessage and MyEventHanlder will get called?

Yes. PreFilterMessage will be called first. Here you can examine the
message, and possibly cancel it. If you don't cancel the message, it will
reach the Form class, which will send it to the Button object, and the
Button will in turn raise its Click event, which will then call your
MyEventHanlder.
 
N

Nash

[...] I am right now intrested
in loging Button Click only. But i have one question suppose if i am
having a button and its event handler in the class and i am doing the
hook also. Will PreFilterMessage and MyEventHanlder will get called?

    Yes. PreFilterMessage will be called first. Here you can examine the
message, and possibly cancel it. If you don't cancel the message, it will
reach the Form class, which will send it to the Button object, and the
Button will in turn raise its Click event, which will then call your
MyEventHanlder.

thanks alberto it really helped me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top