changeset 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 a01d9df88d74
files Implab.Fx/StaApartment.cs Implab/Implab.csproj Implab/SyncContextPromise.cs Implab/SyncContextPromiseT.cs
diffstat 4 files changed, 58 insertions(+), 20 deletions(-) [+]
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();
--- a/Implab/Implab.csproj	Mon Mar 20 17:44:18 2017 +0300
+++ b/Implab/Implab.csproj	Tue Mar 21 17:29:13 2017 +0300
@@ -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" />
--- a/Implab/SyncContextPromise.cs	Mon Mar 20 17:44:18 2017 +0300
+++ b/Implab/SyncContextPromise.cs	Tue Mar 21 17:29:13 2017 +0300
@@ -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);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/SyncContextPromiseT.cs	Tue Mar 21 17:29:13 2017 +0300
@@ -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);
+        }
+    }
+}
+