History

GSF.Core: Added ShutdownHandler so ScheduledTask objects can be signaled and disposed of when the application shuts down. Also modified the WeakAction class to be more intuitive.

chishose committed 10 years ago

Commit 74b3a471 parent c6557e811 files · +728 −278Browse files

Showing the changes to Source/Libraries/GSF.Core.Shared/Threading/ThreadContainerBase.cs (1 of 11 files). Show all

Unified · Split

Source/Libraries/GSF.Core.Shared/Threading/ThreadContainerBase.cs+27−9
@@ -57,7 +57,7 @@ namespace GSF.Threading · 56 unmodified lines
57 /// <summary>57 /// <summary>
58 /// State variables for the internal state machine.58 /// State variables for the internal state machine.
59 /// </summary>59 /// </summary>
60 static class State60 private static class State
61 {61 {
62 /// <summary>62 /// <summary>
63 /// Indicates that the task is not running.63 /// Indicates that the task is not running.
@@ -92,30 +92,35 @@ namespace GSF.Threading · 28 unmodified lines
92 public const int Invalid = -1;92 public const int Invalid = -1;
93 }93 }
94 94
95 volatile bool m_runAgain;95 private volatile bool m_runAgain;
96 96
97 /// <summary>97 /// <summary>
98 /// A value less than 0 means false. 98 /// A value less than 0 means false.
99 /// </summary>99 /// </summary>
100 volatile int m_runAgainAfterDelay;100 private volatile int m_runAgainAfterDelay;
101 101
102 volatile int m_state;102 private volatile int m_state;
103 103
104 WeakAction<CallbackArgs> m_callback;104 private bool m_disposeOnShutdown;
105 105
106 CallbackArgs m_args;106 private readonly WeakAction<CallbackArgs> m_callback;
107 private readonly WeakAction m_disposeAndWaitCallback;
107 108
108 volatile bool m_startDisposalCallSuccessful = false;109 private CallbackArgs m_args;
109 110
110 protected ThreadContainerBase(WeakAction<CallbackArgs> callback)111 private volatile bool m_startDisposalCallSuccessful = false;
112
113 protected ThreadContainerBase(Action<CallbackArgs> callback, Action disposeAndWaitCallback, bool disposeOnShutdown)
111 {114 {
115 m_disposeOnShutdown = disposeOnShutdown;
112 m_runAgain = false;116 m_runAgain = false;
113 m_runAgainAfterDelay = -1;117 m_runAgainAfterDelay = -1;
114 118
115 m_args = new CallbackArgs();119 m_args = new CallbackArgs();
116 m_args.Clear();120 m_args.Clear();
117 121
118 m_callback = callback;122 m_callback = new WeakAction<CallbackArgs>(callback);
123 m_disposeAndWaitCallback = new WeakAction(disposeAndWaitCallback);
119 m_state = State.NotRunning;124 m_state = State.NotRunning;
120 }125 }
121 126
@@ -295,6 +300,19 @@ namespace GSF.Threading · 173 unmodified lines
295 }300 }
296 }301 }
297 302
303 protected void Shutdown()
304 {
305 //If the caller did not want to be notified on shutdown
306 // or they did but the callback failed.
307 // clear all callbacks and initiate a disposal.
308 if (!m_disposeOnShutdown && !m_disposeAndWaitCallback.TryInvoke())
309 {
310 m_callback.Clear();
311 m_disposeAndWaitCallback.Clear();
312 StartDisposal();
313 }
314 }
315
298 protected abstract void InternalStart_FromWorkerThread();316 protected abstract void InternalStart_FromWorkerThread();
299 protected abstract void InternalStart_FromWorkerThread(int delay);317 protected abstract void InternalStart_FromWorkerThread(int delay);
300 protected abstract void InternalDispose_FromWorkerThread();318 protected abstract void InternalDispose_FromWorkerThread();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t