Click here to Skip to main content
15,508,758 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: C# syntax I wish for Pin
Super Lloyd26-Sep-16 21:08
Super Lloyd26-Sep-16 21:08 
GeneralRe: C# syntax I wish for Pin
Pete O'Hanlon26-Sep-16 21:22
mvaPete O'Hanlon26-Sep-16 21:22 
GeneralRe: C# syntax I wish for Pin
Super Lloyd26-Sep-16 21:51
Super Lloyd26-Sep-16 21:51 
GeneralRe: C# syntax I wish for Pin
Anurag Gandhi26-Sep-16 21:36
professionalAnurag Gandhi26-Sep-16 21:36 
AnswerRe: C# syntax I wish for Pin
Super Lloyd26-Sep-16 21:57
Super Lloyd26-Sep-16 21:57 
GeneralRe: C# syntax I wish for Pin
Anurag Gandhi26-Sep-16 22:19
professionalAnurag Gandhi26-Sep-16 22:19 
GeneralRe: C# syntax I wish for Pin
Super Lloyd26-Sep-16 22:30
Super Lloyd26-Sep-16 22:30 
GeneralRe: C# syntax I wish for Pin
Yortw28-Sep-16 12:12
Yortw28-Sep-16 12:12 
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<EventArgs>(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.
C#
public static class EventExtensions
    {
        public static Task<T> WaitForEvent<T>(this object eventSource, string eventName) where T : EventArgs
        {
            var tcs = new System.Threading.Tasks.TaskCompletionSource<T>();
            var eventInfo = eventSource.GetType().GetEvent(eventName);

            EventHandler<T> eventHandler = null;
            eventHandler = new EventHandler<T>(
                (source, e) =>
                {
                    eventInfo.RemoveEventHandler(eventSource, eventHandler);
                    tcs.TrySetResult(e);
                }
            );
            eventInfo.AddEventHandler(eventSource, eventHandler);

            return tcs.Task;
        }
    }

PraiseRe: C# syntax I wish for Pin
Super Lloyd28-Sep-16 14:55
Super Lloyd28-Sep-16 14:55 
GeneralRe: C# syntax I wish for Pin
Yortw28-Sep-16 14:57
Yortw28-Sep-16 14:57 
GeneralRe: C# syntax I wish for Pin
Pete O'Hanlon26-Sep-16 23:02
mvaPete O'Hanlon26-Sep-16 23:02 
GeneralRe: C# syntax I wish for Pin
BillWoodruff27-Sep-16 2:02
mveBillWoodruff27-Sep-16 2:02 
GeneralRe: C# syntax I wish for Pin
Super Lloyd27-Sep-16 3:21
Super Lloyd27-Sep-16 3:21 
GeneralRe: C# syntax I wish for Pin
Shuqian Ying26-Sep-16 23:42
Shuqian Ying26-Sep-16 23:42 
GeneralRe: C# syntax I wish for Pin
Super Lloyd27-Sep-16 3:22
Super Lloyd27-Sep-16 3:22 
GeneralRe: C# syntax I wish for Pin
Member 1052748428-Sep-16 20:19
Member 1052748428-Sep-16 20:19 
GeneralRe: C# syntax I wish for Pin
Super Lloyd28-Sep-16 22:06
Super Lloyd28-Sep-16 22:06 
GeneralKeyboards Pin
milo-xml26-Sep-16 10:49
professionalmilo-xml26-Sep-16 10:49 
GeneralRe: Keyboards Pin
User 842026-Sep-16 11:04
User 842026-Sep-16 11:04 
GeneralRe: Keyboards Pin
milo-xml26-Sep-16 11:11
professionalmilo-xml26-Sep-16 11:11 
GeneralRe: Keyboards Pin
Clifford Nelson26-Sep-16 11:25
Clifford Nelson26-Sep-16 11:25 
GeneralRe: Keyboards Pin
milo-xml27-Sep-16 2:13
professionalmilo-xml27-Sep-16 2:13 
GeneralRe: Keyboards Pin
Clifford Nelson27-Sep-16 5:44
Clifford Nelson27-Sep-16 5:44 
GeneralRe: Keyboards Pin
David Crow26-Sep-16 11:30
David Crow26-Sep-16 11:30 
GeneralRe: Keyboards Pin
Sander Rossel26-Sep-16 12:33
professionalSander Rossel26-Sep-16 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

An amazing AI programming tool is invented that can do one task perfectly. What would you choose?
  Results   145 votes