# HG changeset patch # User cin # Date 1523569390 -10800 # Node ID 440801d88019428b34a8ca73003b3028b9f247c5 # Parent c52691faaf21a49ce4132f9794009e9dfa57b033 working on runnable components diff -r c52691faaf21 -r 440801d88019 Implab/Components/RunnableComponent.cs --- a/Implab/Components/RunnableComponent.cs Wed Apr 11 03:05:14 2018 +0300 +++ b/Implab/Components/RunnableComponent.cs Fri Apr 13 00:43:10 2018 +0300 @@ -12,7 +12,7 @@ /// This class provides a basic lifecycle from the creation to the /// termination of the component. /// - public class RunnableComponent : IAsyncComponent, IRunnable, IInitializable, IDisposable { + public abstract class RunnableComponent : IAsyncComponent, IRunnable, IInitializable, IDisposable { /// /// This class bounds lifetime to the task, @@ -93,6 +93,15 @@ ExecutionState m_state; + /// + /// Объект синхронизации используется для обеспечения совместного доступа + /// клиента компоненты и процессов, протекающих внутри компоненты, к общему + /// состоянию, т.е.true таким свойствам, как , + /// . Обработчики события + /// вызываются уже с установленной блокировкой, поэтому дополнительная + /// синхронизация не требуется. + /// + public object SynchronizationObject { get { return m_lock; } } protected RunnableComponent(bool initialized) { State = initialized ? ExecutionState.Ready : ExecutionState.Created; @@ -117,6 +126,11 @@ public Exception LastError { get; private set; } + /// + /// Событие изменения состояния компоненты.see Обработчики данного события + /// вызываются внутри блокировки и должны + /// выполняться максимально быстро. + /// public event EventHandler StateChanged; /// @@ -130,6 +144,13 @@ /// public void Dispose() { bool dispose = false; + lock (SynchronizationObject) { + if (m_state != ExecutionState.Disposed) { + dispose = true; + m_state = ExecutionState.Disposed; + m_cookie = new object(); + } + } if (dispose) { Dispose(true); GC.SuppressFinalize(this); @@ -152,7 +173,7 @@ public void Initialize() { var cookie = new object(); if (MoveInitialize(cookie)) - ScheduleTask(InitializeInternalAsync, CancellationToken.None, cookie); + Safe.NoWait(ScheduleTask(InitializeInternalAsync, CancellationToken.None, cookie)); else throw new InvalidOperationException(); } @@ -173,19 +194,36 @@ public void Start(CancellationToken ct) { var cookie = new object(); if (MoveStart(cookie)) - ScheduleTask(StartInternalAsync, ct, cookie); + Safe.NoWait(ScheduleStartAndRun(ct, cookie)); else throw new InvalidOperationException(); } + async Task ScheduleStartAndRun(CancellationToken ct, object cookie) { + try { + await ScheduleTask(StartInternalAsync, ct, cookie); + RunInternal(); + } catch (Exception err) { + Fail(err); + } + } + protected virtual Task StartInternalAsync(CancellationToken ct) { return Task.CompletedTask; } + /// + /// This method is called after the component is enetered running state, + /// use this method to + /// + protected virtual void RunInternal() { + + } + public void Stop(CancellationToken ct) { var cookie = new object(); if (MoveStop(cookie)) - ScheduleTask(StopAsync, ct, cookie); + Safe.NoWait(ScheduleTask(StopAsync, ct, cookie)); else throw new InvalidOperationException(); } @@ -273,9 +311,9 @@ } } - void ScheduleTask(Func next, CancellationToken ct, object cookie) { + Task ScheduleTask(Func next, CancellationToken ct, object cookie) { - m_current = AsyncOperationDescriptor.Create(async (x) => { + var op = AsyncOperationDescriptor.Create(async (x) => { try { await next(x); MoveSuccess(cookie); @@ -283,6 +321,9 @@ MoveFailed(e, cookie); } }, ct); + + m_current = op; + return op.Task; } #endregion