Task返回函数中调用Task.Result是否阻塞调用线程
Whether the calling thread will be blocked if Task.Result is called in the Task returning function
我尽量不在我的代码中创建冗余的 Task
对象,并且我在可能的情况下编写 Task
returning 函数而不是 async Task
函数。
当需要通过 async
函数保存值 return 时,我不得不创建函数 return async Task
并使用 [= 调用函数19=].
示例:
async Task SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
}
我写的是:
Task SomeWorkAsync()
{
var task = AnotherWorkAsync();
someGlobalVariable = task.Result;
return task;
}
但我担心它会像同步代码那样阻塞调用线程。
await SomeWorkAsync(); //main thread block
是否有另一种方法可以重写示例中的代码,而不用像 async
关键字那样用新的 Task
包装整个函数?
如果你想为自己保存状态机,只需调用
public Task SomeWorkAsync()
{
...
return AnotherWorkAsync();
}
不要调用 Result
,只需 return 方法定义中没有 async
关键字的任务即可
简而言之,您只是在return执行一项任务,可以在更高层等待
更新
Yes this does the trick if AnotherWorkAsync is async Task but in my
case it is async Task<T>
public Task<MyAwesomeType> SomeWorkAsync()
{
...
return AnotherWorkAsync();
}
更新 2
This is still not what I mean. Class which will call SomeWorkAsync
doesn't know anything about private variable someGlobalVariable. It is
necessary to get value and set this variable inside
public async Task SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
}
如果您需要异步方法的结果,您将必须await
它。
尽量不要混合使用同步代码和 async
await
模式,您正在使用 Result
,这可能会导致死锁...让 async
传播
I try to not create redundant Task objects in my code and I write Task returning functions instead of async Task functions where it is possible.
这不常见,也不是使用 TPL 的预期方式。
这是错误的:
Task SomeWorkAsync()
{
var task = AnotherWorkAsync();
someGlobalVariable = task.Result;
return task;
}
你应该使用
async Task<T> SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
return someGlobalVariable;
}
只有在严格的情况下才应该使用 .Result
来获得 Task
的结果。
回答你的问题:
- 是的,调用
.Result
会阻塞您的线程。
- 请参阅我关于为什么我认为使用 await 而不是 return 任务更好的评论:
我尽量不在我的代码中创建冗余的 Task
对象,并且我在可能的情况下编写 Task
returning 函数而不是 async Task
函数。
当需要通过 async
函数保存值 return 时,我不得不创建函数 return async Task
并使用 [= 调用函数19=].
示例:
async Task SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
}
我写的是:
Task SomeWorkAsync()
{
var task = AnotherWorkAsync();
someGlobalVariable = task.Result;
return task;
}
但我担心它会像同步代码那样阻塞调用线程。
await SomeWorkAsync(); //main thread block
是否有另一种方法可以重写示例中的代码,而不用像 async
关键字那样用新的 Task
包装整个函数?
如果你想为自己保存状态机,只需调用
public Task SomeWorkAsync()
{
...
return AnotherWorkAsync();
}
不要调用 Result
,只需 return 方法定义中没有 async
关键字的任务即可
简而言之,您只是在return执行一项任务,可以在更高层等待
更新
Yes this does the trick if AnotherWorkAsync is async Task but in my case it is async
Task<T>
public Task<MyAwesomeType> SomeWorkAsync()
{
...
return AnotherWorkAsync();
}
更新 2
This is still not what I mean. Class which will call SomeWorkAsync doesn't know anything about private variable someGlobalVariable. It is necessary to get value and set this variable inside
public async Task SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
}
如果您需要异步方法的结果,您将必须await
它。
尽量不要混合使用同步代码和 async
await
模式,您正在使用 Result
,这可能会导致死锁...让 async
传播
I try to not create redundant Task objects in my code and I write Task returning functions instead of async Task functions where it is possible.
这不常见,也不是使用 TPL 的预期方式。
这是错误的:
Task SomeWorkAsync()
{
var task = AnotherWorkAsync();
someGlobalVariable = task.Result;
return task;
}
你应该使用
async Task<T> SomeWorkAsync()
{
someGlobalVariable = await AnotherWorkAsync();
return someGlobalVariable;
}
只有在严格的情况下才应该使用 .Result
来获得 Task
的结果。
回答你的问题:
- 是的,调用
.Result
会阻塞您的线程。 - 请参阅我关于为什么我认为使用 await 而不是 return 任务更好的评论: