hardywang
hardywang

Reputation: 5162

C# Async void of event handler got warning/error

enter image description here

I understand the async should not use void as return type unless it is an event handler. But I have code snippet above, when I turn on warning as error in my project setting I got the error above when I compile code.

RECS0165 Asynchronous method '' should not return void

If I remove async then I get another compile error

The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

The suggested fix is to add async to the anonymous function. It is a dead lock.

Do I do something wrong here?

The steps to reproduce the issue:

Upvotes: 0

Views: 1145

Answers (2)

Nkosi
Nkosi

Reputation: 247133

The Tick event handler delegate is incorrect.

Use

async (sender, e) => ...

or use an EventArg derived class for e

async (object sender, EventArgs e) => ...    

What you currently have is an anonymous object that you are trying to assign as an event handler. The compiler does not allow that hence the error.

Upvotes: 3

Related Questions