Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/tray_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,26 @@ int tray_init(struct tray *tray) {

int tray_loop(int blocking) {
MSG msg;
int has_message = 0;

// Use the thread queue and only dispatch when a message was actually retrieved.
// This ensures WM_QUIT is observed and avoids dispatching an uninitialized MSG.
if (blocking) {
GetMessage(&msg, hwnd, 0, 0);
has_message = GetMessage(&msg, NULL, 0, 0);
if (has_message <= 0) {
return -1;
}
} else {
PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE);
has_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (has_message == 0) {
return 0;
}
}

if (msg.message == WM_QUIT) {
return -1;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
return 0;
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@ TEST_F(TrayTest, TestTrayLoop) {
EXPECT_EQ(result, 0);
}

#if defined(TRAY_WINAPI)
TEST_F(TrayTest, TestTrayLoopHandlesThreadQuitMessage) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);

// WM_QUIT is posted to the thread queue, not to a specific window.
PostQuitMessage(0);

bool sawQuit = false;
for (int i = 0; i < 200; ++i) {
if (tray_loop(0) == -1) {
sawQuit = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

EXPECT_TRUE(sawQuit);
}
#endif

TEST_F(TrayTest, TestTrayUpdate) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
Expand Down
Loading