summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-06-01 19:17:58 +0200
committerStanislaw Halik <sthalik@misaki.pl>2015-06-01 19:17:58 +0200
commitf27e117064d4718003157caa26d3da54b9d19f69 (patch)
tree0a9d68ebd11e5f2144d956ba7704807e867b4636
parentfcaf48603f50558326c155edec47fbd64fb09ab9 (diff)
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.
-rw-r--r--facetracknoir/process_detector.cpp87
1 files 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 <windows.h>
#include <TlHelp32.h>
+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;
}