我在这里搜索了StackOverflow和其他几个地方,但从未找到确切答案。在WPF应用程序中,我正在尝试使用lambda多行代码块来实现任务,并且我得到“并非所有代码路径都返回值”。如何从此返回Func<string>
?
private async void btnLongOperation_Click(object sender, RoutedEventArgs e)
{
lblResult.Content = "Commencing long-running operation...";
var task = Task.Run<string>(() =>
{
Thread.Sleep(3000);
lblResult.Content = "Finishing long-running operation...";
});
lblResult.Content = await task;
}
Error 1 Not all code paths return a value in lambda expression of type 'System.Func<string>'
答案 0 :(得分:3)
Task.Run<string>
提供Func<string>
类型的输入。这意味着它没有输入(()
)并且必须返回一个字符串。所以这可能有所帮助:
var task = Task.Run<string>(() =>
{
Thread.Sleep(3000);
//Here
return "Finishing long-running operation...";
});