From f27e117064d4718003157caa26d3da54b9d19f69 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 1 Jun 2015 19:17:58 +0200 Subject: process-detector: fix API usage and automatic restart Issue: #160 API usage caused snapshot requests to fail most of the time. Fix that. We now no longer restart if user manually stopped. In such case, wait until the application exits before resuming auto-restart behavior. --- facetracknoir/process_detector.cpp | 87 ++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 50 deletions(-) diff --git a/facetracknoir/process_detector.cpp b/facetracknoir/process_detector.cpp index 52d5de6d..a947ab19 100644 --- a/facetracknoir/process_detector.cpp +++ b/facetracknoir/process_detector.cpp @@ -149,8 +149,33 @@ void process_detector::remove() #include #include +static QStringList get_all_executable_names() +{ + QStringList ret; + HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (h == INVALID_HANDLE_VALUE) + return ret; + + PROCESSENTRY32 e; + e.dwSize = sizeof(e); + + if (Process32First(h, &e) != TRUE) + { + CloseHandle(h); + return ret; + } + + do { + ret.append(e.szExeFile); + } while (Process32Next(h, &e) == TRUE); + + CloseHandle(h); + + return ret; +} + bool process_detector_worker::should_stop() -{ +{ if (last_exe_name == "") return false; @@ -162,34 +187,12 @@ bool process_detector_worker::should_stop() return false; } - HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (h == INVALID_HANDLE_VALUE) - return false; + QStringList exe_list = get_all_executable_names(); - PROCESSENTRY32 e; - - if (Process32First(h, &e) == TRUE) - { - QString exe_name(e.szExeFile); - if (exe_name == last_exe_name) - { - CloseHandle(h); - return false; - } - } - - while (Process32Next(h, &e) == TRUE) - { - QString exe_name(e.szExeFile); - if (exe_name == last_exe_name) - { - CloseHandle(h); - return false; - } - } + if (exe_list.contains(last_exe_name)) + return false; last_exe_name = ""; - CloseHandle(h); return true; } @@ -204,38 +207,22 @@ bool process_detector_worker::config_to_start(QString& str) } auto filenames = s.split_process_names(); + QStringList exe_list = get_all_executable_names(); - HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (h == INVALID_HANDLE_VALUE) + // assuming manual stop by user button click. + // don't automatically start again while the same process is running. + if (last_exe_name != "" && exe_list.contains(last_exe_name)) return false; - PROCESSENTRY32 e; - - if (Process32First(h, &e) == TRUE) - { - QString exe_name(e.szExeFile); - if (filenames.contains(exe_name)) - { - str = filenames[exe_name]; - last_exe_name = exe_name; - CloseHandle(h); - return true; - } - } - - while (Process32Next(h, &e) == TRUE) + for (auto& name : exe_list) { - QString exe_name(e.szExeFile); - if (filenames.contains(exe_name)) + if (filenames.contains(name)) { - str = filenames[exe_name]; - last_exe_name = exe_name; - CloseHandle(h); + last_exe_name = name; + str = filenames[name]; return true; } } - - CloseHandle(h); return false; } -- cgit v1.2.3