diff --git a/Implab.Fx/StaApartment.cs b/Implab.Fx/StaApartment.cs --- a/Implab.Fx/StaApartment.cs +++ b/Implab.Fx/StaApartment.cs @@ -13,6 +13,8 @@ namespace Implab.Fx { public class StaApartment : RunnableComponent { readonly Thread m_worker; SynchronizationContext m_syncContext; + SyncContextPromise m_enterPromise; + readonly Promise m_threadStarted; readonly Promise m_threadTerminated; @@ -34,6 +36,19 @@ namespace Implab.Fx { } } + /// + /// Returns the promise which will dispatch all handlers inside the apartment using it's + /// + /// + /// Current implementation is optimized and will lost aync operation stack + /// + /// The promise + public IPromise Enter() { + if (m_enterPromise == null) + throw new InvalidOperationException(); + return m_enterPromise; + } + public IPromise Invoke(Action action) { Safe.ArgumentNotNull(action, "action"); @@ -156,8 +171,9 @@ namespace Implab.Fx { void WorkerEntry() { m_syncContext = new WindowsFormsSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(m_syncContext); - + m_enterPromise = new SyncContextPromise(m_syncContext); m_threadStarted.Resolve(); + m_enterPromise.Resolve(); Application.OleRequired(); Application.Run(); diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -99,9 +99,10 @@ + - + diff --git a/Implab/SyncContextPromise.cs b/Implab/SyncContextPromise.cs --- a/Implab/SyncContextPromise.cs +++ b/Implab/SyncContextPromise.cs @@ -1,18 +1,21 @@ -using System.Threading; -using System; - -namespace Implab { - public class SyncContextPromise : Promise { - readonly SynchronizationContext m_context; - - public SyncContextPromise(SynchronizationContext context) { - Safe.ArgumentNotNull(context, "context"); - m_context = context; - } - - protected override void SignalHandler(HandlerDescriptor handler, int signal) { - m_context.Post(x => base.SignalHandler(handler, signal), null); - } - } -} - +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace Implab { + public class SyncContextPromise : Promise { + readonly SynchronizationContext m_context; + + public SyncContextPromise(SynchronizationContext context) { + Safe.ArgumentNotNull(context, "context"); + + m_context = context; + } + + protected override void SignalHandler(HandlerDescriptor handler, int signal) { + m_context.Post(x => base.SignalHandler(handler, signal), null); + } + } +} diff --git a/Implab/SyncContextPromiseT.cs b/Implab/SyncContextPromiseT.cs new file mode 100644 --- /dev/null +++ b/Implab/SyncContextPromiseT.cs @@ -0,0 +1,18 @@ +using System.Threading; +using System; + +namespace Implab { + public class SyncContextPromise : Promise { + readonly SynchronizationContext m_context; + + public SyncContextPromise(SynchronizationContext context) { + Safe.ArgumentNotNull(context, "context"); + m_context = context; + } + + protected override void SignalHandler(HandlerDescriptor handler, int signal) { + m_context.Post(x => base.SignalHandler(handler, signal), null); + } + } +} +