How to pass around event as parameter in c#
Posted
by Jerry Liu
on Stack Overflow
See other posts from Stack Overflow
or by Jerry Liu
Published on 2010-05-18T12:40:58Z
Indexed on
2010/05/18
13:10 UTC
Read the original article
Hit count: 221
c#
Am writing unit test for a multi-threading application, where I need to wait until a specific event triggered so that I know the asyn operation is done. E.g. When I call repository.add(something), I wait for event AfterChange before doing any assertion. So I write a util function to do that.
public static void SyncAction(EventHandler event_, Action action_)
{
var signal = new object();
EventHandler callback = null;
callback = new EventHandler((s, e) =>
{
lock (signal)
{
Monitor.Pulse(signal);
}
event_ -= callback;
});
event_ += callback;
lock (signal)
{
action_();
Assert.IsTrue(Monitor.Wait(signal, 10000));
}
}
However, the compiler prevents from passing event out of the class. Is there a way to achieve that?
© Stack Overflow or respective owner