Solved

check if an event is hooked?

Posted on 2008-10-07
6
768 Views
Last Modified: 2013-11-26
Hi experts

I have a BindingSource which has the event CurrentChanged.

Now have to hook/unhook this event dynamically in my code. This alone wouldn't be a problem.
But how can I check if the CurrentChanged event is hooked up?

I tried

if(this.MyBindingSource.CurrentChanged == null)
{

}

but this gives an error which says that I can use CurrentChanged only with += or -=
I'm using .NET 2.0, VS2008, WinForms

Thanks
0
Comment
Question by:Arikael
6 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22658448
Not usre exactly but take a look at

this.Events[eventname].GetInvocationList().Length
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22658451
Sample events:

this.Init += new EventHandler(Default3_Init); // to add
this.Init -= new EventHandler(Default3_Init); // to remove
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22658456
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22658460
you can further check
if (this.Init != null) { .. event is hooked }  else { event is not hooked .. }
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22658514
I tried your code and got the same exception (not with binding source but with normal button control)
i am assuming that the inbuilt microsoft controls dont allow you from checking whether the event has been subscribed to or not

in this case i will recommend you to use a variable holding how many events have been hooked to and when ever a new event is hooked the value will be incremented and vice versa
0
 
LVL 3

Author Comment

by:Arikael
ID: 22658563
@crazyman

your code gives me the same exception as noted in my question.
it seems that you can't check whether the event has been subscribed to or not (see ragi0017's post).

Until now I just "unhooked" my event before I hooked it.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question