diff Implab/Components/RunnableComponent.cs @ 262:f1696cdc3d7a v3 v3.0.8

Added IInitializable.Initialize() overload Added IRunnable.Start(), IRunnable.Start() overloads Fixed cancellation of the current operation when Stop() is called More tests
author cin
date Mon, 16 Apr 2018 02:12:39 +0300
parents 547a2fc0d93e
children
line wrap: on
line diff
--- a/Implab/Components/RunnableComponent.cs	Fri Apr 13 19:15:11 2018 +0300
+++ b/Implab/Components/RunnableComponent.cs	Mon Apr 16 02:12:39 2018 +0300
@@ -171,9 +171,13 @@
         }
 
         public void Initialize() {
+            Initialize(CancellationToken.None);
+        }
+
+        public void Initialize(CancellationToken ct) {
             var cookie = new object();
             if (MoveInitialize(cookie))
-                Safe.NoWait(ScheduleTask(InitializeInternalAsync, CancellationToken.None, cookie));
+                Safe.NoWait(ScheduleTask(InitializeInternalAsync, ct, cookie));
             else
                 throw new InvalidOperationException();
         }
@@ -191,6 +195,10 @@
             return Task.CompletedTask;
         }
 
+        public void Start() {
+            Start(CancellationToken.None);
+        }
+
         public void Start(CancellationToken ct) {
             var cookie = new object();
             if (MoveStart(cookie))
@@ -220,6 +228,10 @@
 
         }
 
+        public void Stop() {
+            Stop(CancellationToken.None);
+        }
+
         public void Stop(CancellationToken ct) {
             var cookie = new object();
             if (MoveStop(cookie))
@@ -230,7 +242,12 @@
 
         async Task StopAsync(CancellationToken ct) {
             m_current.Cancel();
-            await Completion;
+
+            try {
+                await Completion;
+            } catch(OperationCanceledException) {
+                // OK
+            }
 
             ct.ThrowIfCancellationRequested();
 
@@ -302,12 +319,13 @@
             }
         }
 
-        void MoveFailed(Exception err, object cookie) {
+        bool MoveFailed(Exception err, object cookie) {
             lock (m_lock) {
                 if (m_cookie != cookie)
-                    return;
+                    return false;
                 LastError = err;
                 State = ExecutionState.Failed;
+                return true;
             }
         }