【发布时间】:2021-10-27 12:00:50
【问题描述】:
我希望在玩主场景游戏时在后台运行一个非常昂贵的 C# 脚本。
作为代码预期工作流程的示例:
- 睡 3 秒
- 在关卡场景中创建并显示瓦片地图实例
- 睡 3 秒
- 用一些新的精灵更新瓷砖地图
睡眠时间从来都不是恒定的,也无法提前知道:它们是算法。
我想避免:
- c# 代码运行时游戏挂起
【问题讨论】:
标签: c# multithreading godot
我希望在玩主场景游戏时在后台运行一个非常昂贵的 C# 脚本。
作为代码预期工作流程的示例:
睡眠时间从来都不是恒定的,也无法提前知道:它们是算法。
我想避免:
【问题讨论】:
标签: c# multithreading godot
在任务中运行 CPU 密集型代码将防止主 UI 线程锁定。
你应该阅读微软的introduction to asynchronous based programming。
这是我认为您想要的示例:
static async void DoHeavyWork()
{
//Starts a new Task that will NOT block the UI thread.
await Task.Run(async () =>
{
//This simulates the heavy task.
await Task.Delay(3000);
await Dispatcher.BeginInvoke(() =>
{
//Run code on the UI thread here.
});
await Task.Delay(3000);
});
}
如果这有帮助,如果您能将此答案标记为解决方案,我将不胜感激。
【讨论】: