Showing the changes to Source/Libraries/GSF.Core/Threading/ThreadContainerThreadpool.cs (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,102 @@ | ||
| 1 | +//****************************************************************************************************** | |
| 2 | +// ThreadContainerThreadpool.cs - Gbtc | |
| 3 | +// | |
| 4 | +// Copyright © 2014, Grid Protection Alliance. All Rights Reserved. | |
| 5 | +// | |
| 6 | +// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | |
| 7 | +// the NOTICE file distributed with this work for additional information regarding copyright ownership. | |
| 8 | +// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | |
| 9 | +// not use this file except in compliance with the License. You may obtain a copy of the License at: | |
| 10 | +// | |
| 11 | +// http://www.opensource.org/licenses/eclipse-1.0.php | |
| 12 | +// | |
| 13 | +// Unless agreed to in writing, the subject software distributed under the License is distributed on an | |
| 14 | +// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | |
| 15 | +// License for the specific language governing permissions and limitations. | |
| 16 | +// | |
| 17 | +// Code Modification History: | |
| 18 | +// ---------------------------------------------------------------------------------------------------- | |
| 19 | +// 3/8/2014 - Steven E. Chisholm | |
| 20 | +// Generated original version of source code. | |
| 21 | +// | |
| 22 | +// | |
| 23 | +//****************************************************************************************************** | |
| 24 | + | |
| 25 | +using System.Threading; | |
| 26 | + | |
| 27 | +namespace GSF.Threading | |
| 28 | +{ | |
| 29 | + internal class ThreadContainerThreadpool | |
| 30 | + : ThreadContainerBase | |
| 31 | + { | |
| 32 | + /// <summary> | |
| 33 | + /// Handle that is created when telling the threadpool to do a delayed start. | |
| 34 | + /// </summary> | |
| 35 | + private volatile RegisteredWaitHandle m_registeredHandle; | |
| 36 | + | |
| 37 | + /// <summary> | |
| 38 | + /// The reset event that allows the timer to be short circuited. | |
| 39 | + /// </summary> | |
| 40 | + private ManualResetEvent m_waitObject; | |
| 41 | + | |
| 42 | + public ThreadContainerThreadpool(WeakActionFast<CallbackArgs> callback) | |
| 43 | + : base(callback) | |
| 44 | + { | |
| 45 | + m_waitObject = new ManualResetEvent(false); | |
| 46 | + } | |
| 47 | + | |
| 48 | + protected override void InternalStart(int delay) | |
| 49 | + { | |
| 50 | + m_waitObject.Reset(); | |
| 51 | + m_registeredHandle = ThreadPool.RegisterWaitForSingleObject(m_waitObject, BeginRunOnTimer, null, delay, executeOnlyOnce: true); | |
| 52 | + } | |
| 53 | + | |
| 54 | + protected override void InternalStart_FromWorkerThread(int delay) | |
| 55 | + { | |
| 56 | + m_waitObject.Reset(); | |
| 57 | + m_registeredHandle = ThreadPool.RegisterWaitForSingleObject(m_waitObject, BeginRunOnTimer, null, delay, executeOnlyOnce: true); | |
| 58 | + } | |
| 59 | + | |
| 60 | + private void BeginRunOnTimer(object state, bool isTimeout) | |
| 61 | + { | |
| 62 | + //Wait for this race condition to satisfy | |
| 63 | + while (m_registeredHandle == null) | |
| 64 | + ; | |
| 65 | + m_registeredHandle.Unregister(null); | |
| 66 | + m_registeredHandle = null; | |
| 67 | + OnRunning(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + protected override void InternalCancelTimer() | |
| 71 | + { | |
| 72 | + m_waitObject.Set(); | |
| 73 | + } | |
| 74 | + | |
| 75 | + protected override void InternalStart() | |
| 76 | + { | |
| 77 | + ThreadPool.QueueUserWorkItem(BeginRunImmediately); | |
| 78 | + } | |
| 79 | + | |
| 80 | + protected override void InternalStart_FromWorkerThread() | |
| 81 | + { | |
| 82 | + ThreadPool.QueueUserWorkItem(BeginRunImmediately); | |
| 83 | + } | |
| 84 | + | |
| 85 | + private void BeginRunImmediately(object state) | |
| 86 | + { | |
| 87 | + OnRunning(); | |
| 88 | + } | |
| 89 | + | |
| 90 | + protected override void InternalDispose_FromWorkerThread() | |
| 91 | + { | |
| 92 | + m_waitObject.Dispose(); | |
| 93 | + m_waitObject = null; | |
| 94 | + } | |
| 95 | + | |
| 96 | + protected override void InternalDoNothing_FromWorkerThread() | |
| 97 | + { | |
| 98 | + | |
| 99 | + } | |
| 100 | + | |
| 101 | + } | |
| 102 | +} | |