我很难理解 async
以及相应的行为。这是我正在尝试编写的程序的模型,也是我遇到的问题的重现:
namespace AsyncTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("starting....");
MyClass myClass = new MyClass();
myClass.DoSomeWork();
Console.WriteLine("ending....");
Console.ReadLine();
}
}
class MyClass
{
public bool DoSomeWork()
{
return GetTrue().Result;
}
Task<bool> GetTrue()
{
return new Task<bool>(() => true);
}
}
}
当我制作 GetTrue().Result
调用,它永远不会返回。我的Task<bool> GetTrue()
方法是用于重现目的的虚拟方法,但我不太确定为什么它永远不会回来。但如果我有这个它会返回:
namespace AsyncTesting
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("starting....");
MyClass myClass = new MyClass();
myClass.DoSomeWork();
Console.WriteLine("ending....");
Console.ReadLine();
}
}
class MyClass
{
public async void DoSomeWork()
{
await GetTrue();
}
Task<bool> GetTrue()
{
return new Task<bool>(() => true);
}
}
}
问题是,对于我的复制品,我想要初始行为。我不要async void
对于调用方法。我希望能够返回 bool
从我的调用“正常”方法。我怎样才能做到这一点?如何让第一个示例运行并实际返回 bool
来自 DoSomeWork()
?
提前谢谢你。
编辑:感谢您的回答。我认为问题出在哪里,是我的 GetTrue()
方法不是真正的重现。在我的方法中,我没有生成实际的 Task<bool>
目的。真的是调用Task.Factory.FromAsync<bool>()
即创建 Task<bool>
目的。而这正是我不知道如何“开始”的任务。还有其他想法吗?谢谢!
最佳答案
您可能正在寻找:
Task<bool> GetTrue()
{
return Task.FromResult(true);
}
代替:
Task<bool> GetTrue()
{
return new Task<bool>(() => true);
}
在后一种情况下,您甚至没有启动您创建的任务。您需要使用 Task.Run
或 Task.RunSynchronously
启动它。通常您不会直接通过 new Task()
创建任务。查看 Stephen Toub 的 "Task.Factory.StartNew" vs "new Task(...).Start"和 "Task.Run vs Task.Factory.StartNew" .
根据您想要实现的目标,您可能会想这样做:
Task<bool> GetTrue()
{
return Task.Run(() => true);
}
但是,通常这会变成已知的反模式,检查 "Should I expose asynchronous wrappers for synchronous methods? ".
已更新以解决评论:
... the context is a WCF service. The Task.Factory.FromAsync() call is wrapping the Begin...() and End...() functions that the service has pushed to the consumer. But I see no way to actually start the task and it just hangs. ... it is the client-side making the WCF call. The client-side app is a Windows Phone 8 application.
您在 UI 线程上遇到了死锁,因为您正在阻止未决任务的结果(使用 task.Result
)。任务本身无法完成,因为 await
延续异步发布到 WP8 应用程序的 UI 线程的调度程序循环,现在已被阻止。 Stephen Cleary 在 his blog 中详细解释了这一点.为避免这种情况,您应该拥抱 "async all the way down"编程模型。这实际上意味着您也应该让您的 UI 事件和命令处理程序async
,并执行 var result = await task
,而不是 var result = task.Result
在那里。
关于c# - 从同步执行中获取任务结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23599923/