Reputation: 5162
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:
Add following code in in MainPage.Xaml.cs
namespace App4 { using System; using System.Threading.Tasks; using Windows.ApplicationModel.Core; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls;
public sealed partial class MainPage : Page
{
private DispatcherTimer refreshTimer;
public MainPage()
{
this.InitializeComponent();
this.refreshTimer = new DispatcherTimer()
{
Interval = new TimeSpan(0, 0, 30)
};
refreshTimer.Tick += async (sender, e) => { await DisplayMostRecentLocationData(string.Empty); };
}
private async Task DisplayMostRecentLocationData(string s)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
});
}
}
}
Upvotes: 0
Views: 1145
Reputation: 5162
The issue actually comes from Refactoring Essentials.
https://github.com/icsharpcode/RefactoringEssentials/issues/280
Upvotes: 1
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