C# syntax I wish for
-
I believe you're correct in that you can't do this on the event itself, but you could make something nearly as good (depending on your definition of 'nearly as good').
await source.WaitForEvent(nameof(source.Event));
Would be possible. Using nameof in VS2015 means you have any issues with refactoring, though it is a bit uglier than your preferred syntax. Something like the following would implement the method (and returns the EventArgs if you need them). An alternate overload would be needed for non-generic event handler based evevents, but that's not hard.
public static class EventExtensions
{
public static Task WaitForEvent(this object eventSource, string eventName) where T : EventArgs
{
var tcs = new System.Threading.Tasks.TaskCompletionSource();
var eventInfo = eventSource.GetType().GetEvent(eventName);EventHandler eventHandler = null; eventHandler = new EventHandler( (source, e) => { eventInfo.RemoveEventHandler(eventSource, eventHandler); tcs.TrySetResult(e); } ); eventInfo.AddEventHandler(eventSource, eventHandler); return tcs.Task; } }
Man, I can't upvote this enough! Awesome, as good as it get, love it! :-D
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Man, I can't upvote this enough! Awesome, as good as it get, love it! :-D
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Not sure where to post that, (there might some sort of MSDN link somewhere) for now I just post there. "Sometimes" one has to wait for an event to be triggered (once) and then do something, I'd like to write a
WaitForEvent()
method. Unfortunately, due to the particular of event syntax, I can't and I have to write one wait for *that* event method for every single event I am interested in!... :(( Mmm... case for a code sinppet me just think! :omg: :-\ :rolleyes: In any case here is the syntax for a specific event:Task WaitForThatEventAsync(Foo source)
{
var res = new TaskCompletionSource<bool>();
EventHandler eh = null;
eh = (o, e) =>
{
source.ThatEvent -= eh;
res.TrySetResult(true);
};
source.ThatEvent += eh;
return res.Task;
}EDIT My explanation seem confusing.. another explanation is that I would like to be able to write a "general purpose"
WaitForEvent()
extension method that transform this cluncky codeEventHandler ev = null;
ev = (o, e) => {
source.Event -= eh;
DoSomething();
};
source.Event += eh;into this elegant code
await source.Event.WaitForEvent()
DoSomething();where both do the exact same thing! Sadly this is not possible right now... I have a to write a new
WaitForThatEvent()
method for each event I am interested in... :(( but it would be cool. Would make ReactiveC# better looking too.A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
your prince code will come and rescue you someday ;P
-
your prince code will come and rescue you someday ;P
It already had!! :omg: :-\ The Lounge[^]
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!