Make a c# / wpf event fire only once?

Posted by Donnie on Stack Overflow See other posts from Stack Overflow or by Donnie
Published on 2010-06-03T17:25:30Z Indexed on 2010/06/03 17:34 UTC
Read the original article Hit count: 426

Filed under:
|
|

I have a case where I want a given event to execute once, and only once. I'm trying to do it this way, but I'm having prolems (else I wouldn't be asking). Note that the following code is inside the function that decides that the event needs to be fired.

EventHandler h = delegate(Object sender, EventArgs e) {
  FiringControl.TheEvent -= h; // <-- Error, use of unassigned local variable "h"
  // Do stuff
}
FiringControl.TheEvent += h;

In general this should work because of the way scope is preserved for delegates until after they're done running, but, since h is in the process of being built when I try to use it it is still considered to be "uninitialized", apparently.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf