77#include " Command.h"
88#include " ExecutionContext.h"
99#include < winget/Checkpoint.h>
10+ #include < winget/COMStaticStorage.h>
1011#include < winget/Reboot.h>
1112#include < winget/UserSettings.h>
1213#include < winget/NetworkSettings.h>
@@ -22,10 +23,15 @@ namespace AppInstaller::CLI::Execution
2223 // Type to contain the CTRL signal and window messages handler.
2324 struct SignalTerminationHandler
2425 {
25- static SignalTerminationHandler& Instance ()
26+ static std::shared_ptr< SignalTerminationHandler> Instance ()
2627 {
27- static SignalTerminationHandler s_instance;
28- return s_instance;
28+ struct Singleton : public WinRT ::COMStaticStorageBase<SignalTerminationHandler>
29+ {
30+ Singleton () : COMStaticStorageBase(L" WindowsPackageManager.SignalTerminationHandler" ) {}
31+ };
32+
33+ static Singleton s_instance;
34+ return s_instance.Get ();
2935 }
3036
3137 void AddContext (Context* context)
@@ -42,8 +48,14 @@ namespace AppInstaller::CLI::Execution
4248 std::lock_guard<std::mutex> lock{ m_contextsLock };
4349
4450 auto itr = std::find (m_contexts.begin (), m_contexts.end (), context);
45- THROW_HR_IF (E_NOT_VALID_STATE, itr == m_contexts.end ());
46- m_contexts.erase (itr);
51+ if (itr == m_contexts.end ())
52+ {
53+ AICLI_LOG (CLI, Warning, << " SignalTerminationHandler::RemoveContext did not find requested object" );
54+ }
55+ else
56+ {
57+ m_contexts.erase (itr);
58+ }
4759 }
4860
4961 void StartAppShutdown ()
@@ -66,7 +78,6 @@ namespace AppInstaller::CLI::Execution
6678 }
6779#endif
6880
69- private:
7081 SignalTerminationHandler ()
7182 {
7283 if (Runtime::IsRunningAsAdmin () && Runtime::IsRunningInPackagedContext ())
@@ -81,7 +92,7 @@ namespace AppInstaller::CLI::Execution
8192 auto progress = args.Progress ();
8293 if (progress > minProgress)
8394 {
84- SignalTerminationHandler::Instance (). StartAppShutdown ();
95+ SignalTerminationHandler::Instance ()-> StartAppShutdown ();
8596 }
8697 });
8798 }
@@ -110,13 +121,14 @@ namespace AppInstaller::CLI::Execution
110121 // if there's no call to join.
111122 if (m_windowThread.joinable ())
112123 {
113- m_windowThread.join ();
124+ m_windowThread.detach ();
114125 }
115126 }
116127
128+ private:
117129 static BOOL WINAPI StaticCtrlHandlerFunction (DWORD ctrlType)
118130 {
119- return Instance (). CtrlHandlerFunction (ctrlType);
131+ return Instance ()-> CtrlHandlerFunction (ctrlType);
120132 }
121133
122134 static LRESULT WINAPI WindowMessageProcedure (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
@@ -125,7 +137,7 @@ namespace AppInstaller::CLI::Execution
125137 switch (uMsg)
126138 {
127139 case WM_QUERYENDSESSION:
128- SignalTerminationHandler::Instance (). StartAppShutdown ();
140+ SignalTerminationHandler::Instance ()-> StartAppShutdown ();
129141 return TRUE ;
130142 case WM_ENDSESSION:
131143 case WM_CLOSE:
@@ -267,11 +279,11 @@ namespace AppInstaller::CLI::Execution
267279
268280 if (add)
269281 {
270- SignalTerminationHandler::Instance (). AddContext (context);
282+ SignalTerminationHandler::Instance ()-> AddContext (context);
271283 }
272284 else
273285 {
274- SignalTerminationHandler::Instance (). RemoveContext (context);
286+ SignalTerminationHandler::Instance ()-> RemoveContext (context);
275287 }
276288 }
277289
@@ -490,12 +502,12 @@ namespace AppInstaller::CLI::Execution
490502
491503 HWND GetWindowHandle ()
492504 {
493- return SignalTerminationHandler::Instance (). GetWindowHandle ();
505+ return SignalTerminationHandler::Instance ()-> GetWindowHandle ();
494506 }
495507
496508 bool WaitForAppShutdownEvent ()
497509 {
498- return SignalTerminationHandler::Instance (). WaitForAppShutdownEvent ();
510+ return SignalTerminationHandler::Instance ()-> WaitForAppShutdownEvent ();
499511 }
500512#endif
501513
0 commit comments