AdamsLair / duality

a 2D Game Development Framework
https://adamslair.github.io/duality
MIT License
1.41k stars 290 forks source link

Duality using a different SynchronizationContext inside the editor #592

Open Barsonax opened 6 years ago

Barsonax commented 6 years ago

Summary

Currently inside the editor duality uses the WindowsFormsSynchronizationContext. However outside the editor it uses no SynchronizationContext (which means this context is either null or equal to SynchronizationContext. This can cause code to behave in a different way when running outside the editor and most likely it will break if this is the case.

To fix this inconsistency the following solutions could be applied:

  1. Make sure that duality uses no SynchronizationContext inside the editor. However since winforms depends on this this will probably mean you need to run winforms and duality in separate threads. I suspect many things will break if this is done.
  2. Make a custom DualitySynchronizationContext that will invoke the continuations in the OnUpdate loop. When using await this will make Tasks have the same behavior as my PathRequest class in Pathfindax meaning continuations will execute on the mainthread. Make sure this context is only applied outside the editor as it will probably break winforms.
  3. Document this is a difference between running inside the editor and outside.

How to reproduce

Workaround

Analysis

If going for 1:

If going for 2:

If going for 3:

ilexp commented 6 years ago

Note: Some potentially related research regarding synchronization contexts can also be found in issue #295. Might be worth looking at as well, with regard to avoiding deadlock potential in manual synchronization adjustments.

Barsonax commented 6 years ago

In pathfindax iam passing the synchronization context as a constructor parameter here instead of it being static (note that I also added interfaces). I do think such a approach would require a custom awaitable though. On the other hand it removes the evil static state.