Skip to content

Commit

Permalink
windows: use QueueUserAPC
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Aug 30, 2024
1 parent 3a860c7 commit 6268992
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,26 @@ void gral_window_clipboard_paste(gral_window *window, void (*callback)(char cons
CloseClipboard();
}

void gral_window_run_on_main_thread(struct gral_window *window, void (*callback)(void *user_data), void *user_data) {
PostMessage((HWND)window, WM_USER, (WPARAM)callback, (LPARAM)user_data);
struct MainThreadCallbackData {
void (*callback)(void *user_data);
void *user_data;
};

static void CALLBACK main_thread_completion_routine(ULONG_PTR parameter) {
MainThreadCallbackData *callback_data = (MainThreadCallbackData *)parameter;
callback_data->callback(callback_data->user_data);
}

void gral_window_run_on_main_thread(gral_window *window, void (*callback)(void *user_data), void *user_data) {
//PostMessage((HWND)window, WM_USER, (WPARAM)callback, (LPARAM)user_data);
MainThreadCallbackData *callback_data = new MainThreadCallbackData();
callback_data->callback = callback;
callback_data->user_data = user_data;
DWORD thread_id;
GetWindowThreadProcessId((HWND)window, &thread_id);
HANDLE thread = OpenThread(0, FALSE, thread_id);
QueueUserAPC(&main_thread_completion_routine, thread, (ULONG_PTR)callback_data);
CloseHandle(thread);
}

static void CALLBACK timer_completion_routine(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue) {
Expand Down

0 comments on commit 6268992

Please sign in to comment.