Skip to content

Commit

Permalink
System: Move IsProcessRunningCached from MeasureProcess to System
Browse files Browse the repository at this point in the history
  • Loading branch information
brianferguson committed Apr 8, 2021
1 parent b24621f commit db41743
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
33 changes: 1 addition & 32 deletions Library/MeasureProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,6 @@
#include "System.h"
#include <TlHelp32.h>

bool IsProcessRunningCached(const std::wstring& lowercaseName) {
static std::unordered_set<std::wstring> 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)
{
}
Expand All @@ -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;
}
33 changes: 33 additions & 0 deletions Library/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Skin.h"
#include "MeasureNet.h"
#include "../Common/PathUtil.h"
#include <TlHelp32.h>

using namespace Gdiplus;

Expand Down Expand Up @@ -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<std::wstring> 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;
}
2 changes: 2 additions & 0 deletions Library/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit db41743

Please sign in to comment.