diff Implab.Fx/StaApartment.cs @ 211:3eb3255d8cc5 v2

Code review, added a non generic version of SyncContextPromise
author cin
date Tue, 21 Mar 2017 17:29:13 +0300
parents 5dc21f6a3222
children cbe10ac0731e
line wrap: on
line diff
--- a/Implab.Fx/StaApartment.cs	Mon Mar 20 17:44:18 2017 +0300
+++ b/Implab.Fx/StaApartment.cs	Tue Mar 21 17:29:13 2017 +0300
@@ -13,6 +13,8 @@
     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 @@
             }
         }
 
+        /// <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 @@
         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();