Reputation: 24739
I'm tying in to a Process's Exited event. I have a checkbox for that process for that event that determines what happens when the Exited event is run. If I check the checkbox while the Process is running, it gets the Process and adds the Exited event. If the Process is not running, it starts the process and adds the Exited event. If the checkbox is unchecked, regardless if the Process is running or not, I do nothing.
My problem is that if the process is running and I check the box (it gets an event), uncheck the box (nothing happens by design), and re-check the box then the process gets TWO Exited events (that are the exact same method). If I have the Process object, how can I not add an Exited event if it already has one?
Upvotes: 1
Views: 1118
Reputation: 13206
If you must peer into the event you can use reflection:
class Program
{
static void Main(string[] args)
{
var car = new Car();
car.Stopped += car_Stopped;
var evt = car.GetType().GetEvent("Stopped");
if(evt == null) //evt will be null if nothing is registered to the event
car.Stopped += car_Stopped;
car.Stop(); //Prints 'Stopped!' only once
Console.ReadLine();
}
static void car_Stopped(object sender, EventArgs e)
{
Console.WriteLine("Stopped!");
}
}
class Car
{
public event EventHandler<EventArgs> Stopped;
protected void OnStopped()
{
var temp = Stopped;
if(temp != null)
Stopped(this, new EventArgs());
}
public void Stop()
{
OnStopped();
}
}
Upvotes: 0
Reputation: 13523
Try this:
delegate void MyDelegate(string message);
class Foo
{
MyDelegate _delegate = null;
int _count = 0;
public event MyDelegate MySingleDelegateEvent
{
add
{
if (_count == 0)
{
_delegate += value;
_count++;
}
}
remove
{
if (_delegate != null)
{
_delegate -= value;
_count--;
}
}
}
}
Upvotes: 0
Reputation: 54552
According to this Microsoft Forum Post http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/45071852-3a61-4181-9a25-068a8698b8b6/ you can just remove the existing handler before you assign it again.
Upvotes: 2
Reputation: 9399
You dissociate events from delegates with the -=
operator.
Just like you'd do this to bind an event to a delegate:
EventHandler foo = new EventHandler(this._onClick)
myButton.Click += foo;
You could also do this, if you keep that reference somewhere:
myButton.Click -= foo;
And then when the event is triggered it no longer calls foo.
What you're probably doing, when the checkbox gets checked, is something like this:
foo.Exited += new EventHandler(your_method);
This way everytime you check the box, you get a new handler tied to the event. Use just one handler. That way, when the checkbox gets unchecked, you can -=
the delegate you'd associated to the exit event before.
Edit: since all you want is to keep the method referenced by the delegate to keep from doing anything, why don't you tie the delegate to the event at some point other than the checking event, and then use the checkbox state in the method to choose whether you run the rest of the method or halt it with a premature return?
Upvotes: 1
Reputation: 48114
To get an array of all delegates subscribing to an invite just use the GetInvoacationList
method. From there you can check it's length or iterate over it to check how many/which delegates the event will invoke.
Delegate[] delegates = MyEvent.GetInvocationList();
Based on what you're actually trying to do it seems like you could just do something like;
if (MyEvent.GetInvocationList().Length > 0)
or
if (MyEvent.GetInvoationList().Contains(referenceToMyDelegate))
Upvotes: 1