//****************************************************************************************************** // MixedSynchronizedOperation.cs - Gbtc // // Copyright © 2014, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 10/08/2014 - Stephen C. Wills // Generated original version of source code. // //****************************************************************************************************** using System; using System.Threading; namespace GSF.Threading { /// /// Indicates modes of execution for the . /// public enum AsynchronousExecutionMode { /// /// Executes asynchronous operations on the thread pool /// (same as ). /// Short = 0, /// /// Executes asynchronous operations on a dedicated background thread /// (same as ). /// Long = 1 } /// /// Represents an operation that cannot run while it is already in progress. /// /// /// The behavior of asynchronous executions will depend on the value of the /// property. When using the short asynchronous /// execution mode, refer to the class for a /// description of the behavior of this class. When using the long asynchronous execution /// mode, refer to the class. Actions executed /// by this class will always be executed on a background thread, even when using the /// long asynchronous execution mode. /// public class MixedSynchronizedOperation : SynchronizedOperationBase { #region [ Members ] // Fields private int m_asynchronousExecutionMode; private AsynchronousExecutionMode m_currentExecutionMode; #endregion #region [ Constructors ] /// /// Creates a new instance of the class. /// /// The action to be performed during this operation. public MixedSynchronizedOperation(Action action) : base(action) { } /// /// Creates a new instance of the class. /// /// The action to be performed during this operation. /// The action to be performed if an exception is thrown from the action. public MixedSynchronizedOperation(Action action, Action exceptionAction) : base(action, exceptionAction) { } #endregion #region [ Properties ] /// /// Gets or sets the mode of execution used to execute the action asynchronously. /// public AsynchronousExecutionMode AsynchronousExecutionMode { get { return (AsynchronousExecutionMode)Interlocked.CompareExchange(ref m_asynchronousExecutionMode, 0, 0); } set { Interlocked.Exchange(ref m_asynchronousExecutionMode, (int)value); } } /// /// Gets the execution mode of the currently executing action. /// public AsynchronousExecutionMode CurrentExecutionMode { get { return m_currentExecutionMode; } } #endregion #region [ Methods ] /// /// Executes the action on a separate thread. /// protected override void ExecuteActionAsync() { if (AsynchronousExecutionMode == AsynchronousExecutionMode.Short) ExecuteAsyncOnThreadPool(); else ExecuteAsyncOnDedicatedThread(); } private void ExecuteAsyncOnThreadPool() { ThreadPool.QueueUserWorkItem(state => { m_currentExecutionMode = AsynchronousExecutionMode.Short; if (ExecuteAction()) ExecuteActionAsync(); }); } private void ExecuteAsyncOnDedicatedThread() { Thread t = new Thread(() => { m_currentExecutionMode = AsynchronousExecutionMode.Long; while (ExecuteAction()) { if (AsynchronousExecutionMode != AsynchronousExecutionMode.Long) { ExecuteActionAsync(); break; } } }); t.IsBackground = true; t.Start(); } #endregion } }