using System; using System.Threading; namespace FastGithub.Windows.Hosting { /// /// WinForm调度器 /// public interface IWinFormDispatcher { /// /// 获取或设置同步上下文 /// SynchronizationContext? SynchronizationContext { get; set; } /// /// 尝试在同步上下文投递执行委托 /// /// /// bool TryInvoke(Action action) { var context = this.SynchronizationContext; if (context == null || action == null) { return false; } context.Post(state => ((Action)state!)(), action); return false; } /// /// 在同步上下文投递执行委托 /// /// void Invoke(Action action) { var context = this.SynchronizationContext; if (context == null) { throw new InvalidOperationException($"{nameof(SynchronizationContext)} is null"); } if (action != null) { context.Post(state => ((Action)state!)(), action); } } } }