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
5757 /// <summary>
5858 /// State variables for the internal state machine.
5959 /// </summary>
60− static class State
60+ private static class State
6161 {
6262 /// <summary>
6363 /// Indicates that the task is not running.
@@ -92,30 +92,35 @@ namespace GSF.Threading · 28 unmodified lines
9292 public const int Invalid = -1;
9393 }
9494
95− volatile bool m_runAgain;
95+ private volatile bool m_runAgain;
9696
9797 /// <summary>
9898 /// A value less than 0 means false.
9999 /// </summary>
100− volatile int m_runAgainAfterDelay;
100+ private volatile int m_runAgainAfterDelay;
101101
102− volatile int m_state;
102+ private volatile int m_state;
103103
104− WeakAction<CallbackArgs> m_callback;
104+ private bool m_disposeOnShutdown;
105105
106− CallbackArgs m_args;
106+ private readonly WeakAction<CallbackArgs> m_callback;
107+ private readonly WeakAction m_disposeAndWaitCallback;
107108
108− volatile bool m_startDisposalCallSuccessful = false;
109+ private CallbackArgs m_args;
109110
110− protected ThreadContainerBase(WeakAction<CallbackArgs> callback)
111+ private volatile bool m_startDisposalCallSuccessful = false;
112+
113+ protected ThreadContainerBase(Action<CallbackArgs> callback, Action disposeAndWaitCallback, bool disposeOnShutdown)
111114 {
115+ m_disposeOnShutdown = disposeOnShutdown;
112116 m_runAgain = false;
113117 m_runAgainAfterDelay = -1;
114118
115119 m_args = new CallbackArgs();
116120 m_args.Clear();
117121
118− m_callback = callback;
122+ m_callback = new WeakAction<CallbackArgs>(callback);
123+ m_disposeAndWaitCallback = new WeakAction(disposeAndWaitCallback);
119124 m_state = State.NotRunning;
120125 }
121126
@@ -295,6 +300,19 @@ namespace GSF.Threading · 173 unmodified lines
295300 }
296301 }
297302
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+
298316 protected abstract void InternalStart_FromWorkerThread();
299317 protected abstract void InternalStart_FromWorkerThread(int delay);
300318 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