@@ -335,9 +335,45 @@ class DECLSPEC_UUID(CLSID_COMMAND_ENUMERATOR) CommandEnumerator
335335};
336336
337337
338+ class PyManagerOperationInProgress
339+ {
340+ HANDLE hGlobalSem;
341+ bool busy;
342+
343+ bool _create () {
344+ hGlobalSem = CreateSemaphoreExW (NULL , 0 , 1 ,
345+ L" PyManager-OperationInProgress" , 0 , SEMAPHORE_MODIFY_STATE | SYNCHRONIZE);
346+
347+ return (hGlobalSem && GetLastError () != ERROR_ALREADY_EXISTS);
348+ }
349+
350+ public:
351+ PyManagerOperationInProgress ()
352+ {
353+ busy = _create ();
354+ }
355+
356+ ~PyManagerOperationInProgress ()
357+ {
358+ if (hGlobalSem) {
359+ if (!busy) {
360+ ReleaseSemaphore (hGlobalSem, 1 , NULL );
361+ }
362+ CloseHandle (hGlobalSem);
363+ }
364+ }
365+
366+ operator bool ()
367+ {
368+ return hGlobalSem && !busy;
369+ }
370+ };
371+
372+
338373class DECLSPEC_UUID (CLSID_IDLE_COMMAND) IdleCommand
339374 : public RuntimeClass<RuntimeClassFlags<ClassicCom>, IExplorerCommand, IObjectWithSite>
340375{
376+ PyManagerOperationInProgress busy;
341377 std::vector<IdleData> idles;
342378 std::wstring iconPath;
343379 std::wstring title;
@@ -356,19 +392,21 @@ class DECLSPEC_UUID(CLSID_IDLE_COMMAND) IdleCommand
356392 iconPath += L" ,-4" ;
357393 }
358394
359- hr = ReadAllIdleInstalls (idles, HKEY_LOCAL_MACHINE, L" Software\\ Python" , KEY_WOW64_32KEY);
360- if (SUCCEEDED (hr)) {
361- hr = ReadAllIdleInstalls (idles, HKEY_LOCAL_MACHINE, L" Software\\ Python" , KEY_WOW64_64KEY);
362- }
363- if (SUCCEEDED (hr)) {
364- hr = ReadAllIdleInstalls (idles, HKEY_CURRENT_USER, L" Software\\ Python" , 0 );
365- }
395+ if (!busy) {
396+ hr = ReadAllIdleInstalls (idles, HKEY_LOCAL_MACHINE, L" Software\\ Python" , KEY_WOW64_32KEY);
397+ if (SUCCEEDED (hr)) {
398+ hr = ReadAllIdleInstalls (idles, HKEY_LOCAL_MACHINE, L" Software\\ Python" , KEY_WOW64_64KEY);
399+ }
400+ if (SUCCEEDED (hr)) {
401+ hr = ReadAllIdleInstalls (idles, HKEY_CURRENT_USER, L" Software\\ Python" , 0 );
402+ }
366403
367- if (FAILED (hr)) {
368- wchar_t buffer[512 ];
369- swprintf_s (buffer, L" IdleCommand error 0x%08X" , (DWORD)hr);
370- OutputDebugStringW (buffer);
371- idles.clear ();
404+ if (FAILED (hr)) {
405+ wchar_t buffer[512 ];
406+ swprintf_s (buffer, L" IdleCommand error 0x%08X" , (DWORD)hr);
407+ OutputDebugStringW (buffer);
408+ idles.clear ();
409+ }
372410 }
373411 }
374412
@@ -387,10 +425,12 @@ class DECLSPEC_UUID(CLSID_IDLE_COMMAND) IdleCommand
387425 iconPath += L" ,-4" ;
388426 }
389427
390- hr = ReadAllIdleInstalls (idles, hive, root, 0 );
428+ if (!busy) {
429+ hr = ReadAllIdleInstalls (idles, hive, root, 0 );
391430
392- if (FAILED (hr)) {
393- idles.clear ();
431+ if (FAILED (hr)) {
432+ idles.clear ();
433+ }
394434 }
395435 }
396436 #endif
@@ -429,7 +469,7 @@ class DECLSPEC_UUID(CLSID_IDLE_COMMAND) IdleCommand
429469
430470 IFACEMETHODIMP GetState (IShellItemArray *psiItemArray, BOOL fOkToBeSlow , EXPCMDSTATE *pCmdState)
431471 {
432- *pCmdState = idles.size () ? ECS_ENABLED : ECS_HIDDEN ;
472+ *pCmdState = idles.size () ? ECS_ENABLED : ECS_DISABLED ;
433473 return S_OK;
434474 }
435475
0 commit comments