Set current SynchronizationContext before the game loop starts
This fixes the problem that `SynchronizationContext.Current` would be null
during the call to `_EnterTree`, `_Ready` and the first call to `_Process` thus
the task continuations would be scheduled outside the main thread, which is unexpected and might lead to crashes.
With this change, task continuations are scheduled always on the main thread and so async/await can be used without any explicit synchronization, which is what is expected.
Fixes #18849
(cherry picked from commit f25240cfe6
)
This commit is contained in:
parent
9294978596
commit
b95cc40640
|
@ -14,6 +14,7 @@ namespace Godot
|
||||||
public GodotTaskScheduler()
|
public GodotTaskScheduler()
|
||||||
{
|
{
|
||||||
Context = new GodotSynchronizationContext();
|
Context = new GodotSynchronizationContext();
|
||||||
|
SynchronizationContext.SetSynchronizationContext(Context);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected sealed override void QueueTask(Task task)
|
protected sealed override void QueueTask(Task task)
|
||||||
|
@ -57,7 +58,6 @@ namespace Godot
|
||||||
|
|
||||||
public void Activate()
|
public void Activate()
|
||||||
{
|
{
|
||||||
SynchronizationContext.SetSynchronizationContext(Context);
|
|
||||||
ExecuteQueuedTasks();
|
ExecuteQueuedTasks();
|
||||||
Context.ExecutePendingContinuations();
|
Context.ExecutePendingContinuations();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue