Skip to content

Commit

Permalink
Remove use of SetTimer() and use std::this_thead::sleep_for()
Browse files Browse the repository at this point in the history
  • Loading branch information
eschan145 committed Jan 17, 2025
1 parent 1f99433 commit 4e375f6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
2 changes: 1 addition & 1 deletion settings.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
update=500
internet_toggler=false
interval=2
interval=1
45 changes: 9 additions & 36 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ DK_API bool close_application_by_exe(const char* exe_name) {
return terminated;
}

DK_API int monitor_executables(const char* folder_path) {
DK_API int monitor_executables(int interval) {
/*
Begin monitoring and closing of the executables in the given folder path.
Expand All @@ -251,37 +251,10 @@ DK_API int monitor_executables(const char* folder_path) {
*/

int count = 0;
int old_interval = settings.get<int>("interval", 0);

while (running) {
// Search recursively through folder_path and terminate all "*.exe"s

for (const auto& entry :
std::filesystem::directory_iterator(folder_path)) {
// If it's a directory, go through its subfiles
if (entry.is_directory()) {
for (const auto& sub_entry :
std::filesystem::directory_iterator(entry.path())) {
if ((sub_entry.is_regular_file()) &&
(sub_entry.path().extension() == ".exe")) {
bool result = dieknow::close_application_by_exe(
sub_entry.path().filename().string().c_str()
);
if (result)
count++;
}
}
break;

} else {
validate();
}
}



// Minimize CPU usage
// std::this_thread::sleep_for(std::chrono::seconds(interval));
dieknow::sweep();
std::this_thread::sleep_for(std::chrono::seconds(interval));
}
return count;
}
Expand Down Expand Up @@ -311,8 +284,7 @@ bool taskkill(DWORD identifier) {
return false;
}

void CALLBACK sweep(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
std::cout << "Sweeping\n";
void sweep() {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (snapshot == INVALID_HANDLE_VALUE) {
Expand Down Expand Up @@ -396,12 +368,15 @@ DK_API void start_monitoring(const char* folder_path) {

running = true;

// std::thread thread(dieknow::monitor_executables, folder_path);
// HANDLE handle = reinterpret_cast<HANDLE>(thread.native_handle());
std::thread thread(dieknow::monitor_executables);
HANDLE handle = reinterpret_cast<HANDLE>(thread.native_handle());

std::cout << "Created monitoring std::thread and retrieved HANDLE.\n";

thread.detach();

int interval = settings.get<int>("interval", 0);
// int old_interval = settings.get<int>("interval", 0);
// if (old_interval != interval) {
// std::cout << "Monitoring interval updated to " << interval
// << " seconds.\n";
Expand Down Expand Up @@ -434,8 +409,6 @@ DK_API void stop_monitoring() {
Stop monitoring executables.
*/

KillTimer(nullptr, SWEEP_TIMER_ID);

// Although just a variable is set to false, because the DieKnow process is
// in a separate thread it will finish immediately.

Expand Down
4 changes: 2 additions & 2 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace dieknow {

bool taskkill(DWORD identifier);

void CALLBACK sweep(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
void sweep();

extern "C" {
extern bool running;
Expand All @@ -86,7 +86,7 @@ extern "C" {
DK_API const char* get_folder_path();
DK_API void start_monitoring(const char* folder_path = FOLDER_PATH);
DK_API void stop_monitoring();
DK_API int monitor_executables(const char* folder_path = FOLDER_PATH);
DK_API int monitor_executables();
DK_API bool close_application_by_exe(const char* exe_name);
DK_API int get_killed_count();
DK_API bool is_running();
Expand Down

0 comments on commit 4e375f6

Please sign in to comment.