# HG changeset patch
# User cin
# Date 2017-03-21 14:29:13
# Node ID 3eb3255d8cc5fd3afe82cac44fc5fe7fda6d5fc8
# Parent  5dc21f6a32228c7cf5665e43b2b1ef637de288a2

Code review, added a non generic version of SyncContextPromise

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 {
             }
         }
 
+        /// <summary>
+        /// Returns the promise which will dispatch all handlers inside the apartment using it's <see cref="SynchronizationContext"/>
+        /// </summary>
+        /// <remarks>
+        /// Current implementation is optimized and will lost aync operation stack
+        /// </remarks>
+        /// <returns>The promise</returns>
+        public IPromise Enter() {
+            if (m_enterPromise == null)
+                throw new InvalidOperationException();
+            return m_enterPromise;
+        }
+
         public IPromise Invoke(Action<ICancellationToken> 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 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Parallels\AsyncPool.cs" />
     <Compile Include="Safe.cs" />
+    <Compile Include="SyncContextPromise.cs" />
     <Compile Include="ValueEventArgs.cs" />
     <Compile Include="PromiseExtensions.cs" />
-    <Compile Include="SyncContextPromise.cs" />
+    <Compile Include="SyncContextPromiseT.cs" />
     <Compile Include="Diagnostics\OperationContext.cs" />
     <Compile Include="Diagnostics\TraceContext.cs" />
     <Compile Include="Diagnostics\LogEventArgs.cs" />
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<T> : Promise<T> {
-        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<T> : Promise<T> {
+        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);
+        }
+    }
+}
+