5

I researched the asynch and await syntax here and here. It really helps to understand the usage but I found an intriguing syntax example on MSDN which I just don't understand.

Question: Could someone please explain to me the syntax of this System.Timers.Timer event registration with asynch await: Why can you use the async await keywords already in the lambda expression?

Timer timer = new Timer(1000);
timer.Elapsed += async ( sender, e ) => await HandleTimer();

private Task HandleTimer()
{
    Console.WriteLine("\nHandler not implemented..." );        
}

Question 2: And what are the two parameters sender & e good for if they don't appear in the HandleTimer method?

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 1
    it's just an anonymous method declared using lambda syntax. It also happens to be asynchronous. Which part are you confused about? – Jonesopolis Jun 07 '16 at 12:40
  • I think the lambda syntax got me confused – Mong Zhu Jun 07 '16 at 13:03
  • 2
    could the people please explain the downvotes to avoid it in future? – Mong Zhu Jun 07 '16 at 13:06
  • People like to jump on the upvote/downvote train pretty quickly. I'd say in the future though, try to explain what *exactly* you don't understand. Literally which part of the expression. – Jonesopolis Jun 07 '16 at 13:15

3 Answers3

12

It assigns an async lambda to the Elapsed event of timer. You can understand the async lambda this way: first, the following is a lambda:

(sender, e) => HandleTimer()

this lambda calls HandleTimer synchronously. Then we add an await to call HandleTimer asynchronously:

(sender, e) => await HandleTimer()

but this won't work because to call something asynchronously you have to be asynchronous yourself, hence the async keyword:

async (sender, e) => await HandleTimer()
ycsun
  • 1,825
  • 1
  • 13
  • 21
  • this is a great explanation. Now I get it, and the two parameters `sender` and `e` are the input parameters for the `HandleTimer` method. – Mong Zhu Jun 07 '16 at 13:05
  • 1
    @MongZhu `async (sender, e) => await HandleTimer(sender, e)` – Patrick Hofman Jun 07 '16 at 13:11
  • ok that actually looks comprehensive. That would mean that if the `HandleTimer` would have no parameters then the event can only be fire inside the class am I right? – Mong Zhu Jun 07 '16 at 13:19
3

This is just an asynchronous lambda expression. It's equivalent to:

timer.Elapsed = CallHandleTimer;

async void CallHandleTimer(object sender, EventArgs e)
{
    await HandleTimer();
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
2

The code you've given is an anonymous function written as a lambda expression.

So what's really happening is that for the timer elapsed event you're assigning the EventHandler as async ( sender, e ) => await HandleTimer();.

which translates to something like

timer.Elapsed += AnonFunc;

async void AnonFunc(object sender, EventArgs e)
{
    await HandleTImer();
}

It seems that it's the lambda that's tripping you up.

Barry O'Kane
  • 1,189
  • 7
  • 12