【问题标题】:Godot C# Running in the backgroundGodot C# 在后台运行
【发布时间】:2021-10-27 12:00:50
【问题描述】:

我希望在玩主场景游戏时在后台运行一个非常昂贵的 C# 脚本。

作为代码预期工作流程的示例:

  1. 睡 3 秒
  2. 在关卡场景中创建并显示瓦片地图实例
  3. 睡 3 秒
  4. 用一些新的精灵更新瓷砖地图

睡眠时间从来都不是恒定的,也无法提前知道:它们是算法。

我想避免:

  • c# 代码运行时游戏挂起

【问题讨论】:

    标签: c# multithreading godot


    【解决方案1】:

    在任务中运行 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);
            });
        }
    

    如果这有帮助,如果您能将此答案标记为解决方案,我将不胜感激。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多