From db41743770a5eb0009b1ae6bc8fbdbe00986988f Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 8 Apr 2021 16:21:32 -0600 Subject: [PATCH] System: Move IsProcessRunningCached from MeasureProcess to System --- Library/MeasureProcess.cpp | 33 +-------------------------------- Library/System.cpp | 33 +++++++++++++++++++++++++++++++++ Library/System.h | 2 ++ 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/Library/MeasureProcess.cpp b/Library/MeasureProcess.cpp index 567561915..2d45c2710 100644 --- a/Library/MeasureProcess.cpp +++ b/Library/MeasureProcess.cpp @@ -12,37 +12,6 @@ #include "System.h" #include -bool IsProcessRunningCached(const std::wstring& lowercaseName) { - static std::unordered_set s_Processes; - static ULONGLONG s_LastUpdateTickCount = 0; - const ULONGLONG updateInterval = 250; // ms - - ULONGLONG tickCount = System::GetTickCount64(); - if (tickCount >= (s_LastUpdateTickCount + updateInterval)) - { - s_LastUpdateTickCount = tickCount; - - s_Processes = {}; - HANDLE thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (thSnapshot != INVALID_HANDLE_VALUE) - { - PROCESSENTRY32 processEntry = { sizeof(processEntry) }; - if (Process32First(thSnapshot, &processEntry)) - { - do - { - std::wstring name = processEntry.szExeFile; - StringUtil::ToLowerCase(name); - s_Processes.insert(name); - } while (Process32Next(thSnapshot, &processEntry)); - } - CloseHandle(thSnapshot); - } - } - - return s_Processes.count(lowercaseName); -} - MeasureProcess::MeasureProcess(Skin* skin, const WCHAR* name) : Measure(skin, name) { } @@ -61,5 +30,5 @@ void MeasureProcess::ReadOptions(ConfigParser& parser, const WCHAR* section) void MeasureProcess::UpdateValue() { - m_Value = IsProcessRunningCached(m_ProcessNameLowercase) ? 1.0 : -1.0; + m_Value = System::IsProcessRunningCached(m_ProcessNameLowercase) ? 1.0 : -1.0; } diff --git a/Library/System.cpp b/Library/System.cpp index fadc3d604..51ba3868d 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -12,6 +12,7 @@ #include "Skin.h" #include "MeasureNet.h" #include "../Common/PathUtil.h" +#include using namespace Gdiplus; @@ -1470,3 +1471,35 @@ std::wstring System::GetTemporaryFile(const std::wstring& iniFile) return temporary; } + +bool System::IsProcessRunningCached(const std::wstring& lowercaseName) +{ + static std::unordered_set s_Processes; + static ULONGLONG s_LastUpdateTickCount = 0ULL; + const ULONGLONG updateInterval = 250ULL; // ms + + ULONGLONG tickCount = GetTickCount64(); + if (tickCount >= (s_LastUpdateTickCount + updateInterval)) + { + s_LastUpdateTickCount = tickCount; + + s_Processes = {}; + HANDLE thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (thSnapshot != INVALID_HANDLE_VALUE) + { + PROCESSENTRY32 processEntry = { sizeof(processEntry) }; + if (Process32First(thSnapshot, &processEntry)) + { + do + { + std::wstring name = processEntry.szExeFile; + StringUtil::ToLowerCase(name); + s_Processes.insert(name); + } while (Process32Next(thSnapshot, &processEntry)); + } + CloseHandle(thSnapshot); + } + } + + return s_Processes.count(lowercaseName) != 0; +} diff --git a/Library/System.h b/Library/System.h index ab7372257..03a72a09c 100644 --- a/Library/System.h +++ b/Library/System.h @@ -72,6 +72,8 @@ class System static void UpdateIniFileMappingList(); static std::wstring GetTemporaryFile(const std::wstring& iniFile); + static bool IsProcessRunningCached(const std::wstring& lowercaseName); + private: static void CALLBACK MyWinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime); static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);