[英]Waiting async method inside sync method without blocking the thread
I have some code that runs under Unity Game Engine.我有一些在 Unity 游戏引擎下运行的代码。 Parts of that code is also running on the server (IIS 8.0, Azure Cloud).
该代码的一部分也在服务器(IIS 8.0、Azure Cloud)上运行。 Since Unity only supports a subset of .Net Framework (3.5 or something close to it) it doesn't support at the moment async / await pattern.
由于 Unity 仅支持 .Net Framework 的一个子集(3.5 或接近它的版本),因此目前不支持 async/await 模式。
Naturally my server is fully built by using async methods.当然,我的服务器是完全使用异步方法构建的。 The problem is that Unity code needs to call some of my server methods and it cannot use the await keyword.
问题是 Unity 代码需要调用我的一些服务器方法并且它不能使用 await 关键字。 I need to expose some basic methods for the Unity code so it can call them on the server.
我需要为 Unity 代码公开一些基本方法,以便它可以在服务器上调用它们。
For example I could have the following code:例如,我可以有以下代码:
Public bool Save()
{
Task.Run(async() => await PlayerService.SavePlayer());
return true;
}
This of course runs nicely, but I need to make sure that the method save doesn't return before the save has completed.这当然运行良好,但我需要确保方法 save 在保存完成之前不会返回。 Now how do I wait for that task to run to the end before it returns to the method Save()?
现在我如何等待该任务运行到最后才返回到 Save() 方法? Also do this without blocking the thread?
也可以在不阻塞线程的情况下执行此操作? Blocking the method is fine.
阻止该方法很好。
Btw.顺便说一句。 there is a ton of await calls in side the PlayerService.SavePlayer();
PlayerService.SavePlayer() 中有大量的 await 调用;
Also there is the problem of synchronization context, it seems that when calling the initial call through SignalR it doesn't like if the context changes.还有同步上下文的问题,似乎在通过 SignalR 调用初始调用时,如果上下文发生变化,它就不喜欢。
Just to clarify how SignalR is involved here is an example of the pattern:只是为了澄清 SignalR 在这里是如何参与的,这是模式的一个例子:
SignalR gets a message -> This loads a instance of the model (Unity code) from the memory -> The model then thinks it's a good time save its' state and calls the server methods -> Save() method is called -> Save calls async method in playerservices -> this does it's magic -> this returns to save -> Model sees a that save was success and continues to do its' business. SignalR 收到一条消息 -> 这会从内存中加载模型的实例(Unity 代码) -> 模型认为现在是保存其状态并调用服务器方法的好时机 -> 调用 Save() 方法 -> 保存在 playerservices 中调用异步方法 -> 这很神奇 -> 返回保存 -> 模型看到保存成功并继续做它的业务。
Now how do I wait for that task to run to the end before it returns to the method Save()?
现在我如何等待该任务运行到最后才返回到 Save() 方法?
In this case, you can Wait
the Task
returned by Task.Run
.在这种情况下,你可以
Wait
该Task
通过返回Task.Run
。 This is not an optimal solution (it's a hack), but you can't have an optimal solution unless Unity supports await
.这不是最佳解决方案(这是一个黑客),但除非 Unity 支持
await
否则您无法获得最佳解决方案。
Also do this without blocking the thread?
也可以在不阻塞线程的情况下执行此操作?
That's not possible.那是不可能的。 You're exposing a synchronous method to Unity, and when a synchronous method returns, it's complete.
您正在向 Unity 公开一个同步方法,当同步方法返回时,它就完成了。 So you have to either block the thread or return after starting an asynchronous operation (like your code is already doing).
所以你必须要么阻塞线程,要么在开始异步操作后返回(就像你的代码已经在做的那样)。
Also there is the problem of synchronization context, it seems that when calling the initial call through SignalR it doesn't like if the context changes.
还有同步上下文的问题,似乎在通过 SignalR 调用初始调用时,如果上下文发生变化,它就不喜欢。
SignalR 1.x had some quirks around SynchronizationContext
. SignalR 1.x 有一些关于
SynchronizationContext
怪癖。 SignalR 2.0 was just released (and they were supposed to fix those quirks in that release); SignalR 2.0 刚刚发布(他们应该修复那个版本中的那些怪癖); I'd try upgrading and see if it works better.
我会尝试升级,看看它是否更好用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.