我理解的异步不应使用void
的返回类型,除非它是一个事件处理程序。但我有上面的代码片段,当我打开警告作为我的项目设置中的错误时,我编译代码时出现上述错误。
RECS0165异步方法“”不应该返回void
如果我删除async
然后我再编译错误
的“伺机”运算符只能异步拉姆达内使用 表达。考虑用'async'修饰符标记此lambda表达式。
建议的修复方法是将async
添加到匿名函数。这是一个死锁。
我在这里做错了什么?
重现该问题的步骤:
- 创建VS2017空白UWP项目,它会创建应用程序,XAML和MainPage.xaml中。
添加以下代码在MainPage.xaml.cs中
命名空间APP4 { 使用系统; using System.Threading.Tasks; 使用Windows.ApplicationModel.Core;使用Windows.UI.Core的 ;使用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,() => { }); } }
}
的委托不正确。 'async(sender,e)=>'或使用'EventArg'派生的类用于'e' – Nkosi