From 426097a74a5c67dab5446a483d04b2878b4585a6 Mon Sep 17 00:00:00 2001 From: RamonUnch <74856804+RamonUnch@users.noreply.github.com> Date: Thu, 13 May 2021 09:12:46 +0200 Subject: [PATCH 1/2] Fix infinite loop in tray icon handling Fixes: https://github.com/stefansundin/altdrag/issues/34, https://github.com/stefansundin/altdrag/issues/76 and https://github.com/stefansundin/altdrag/issues/107 --- include/tray.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/tray.c b/include/tray.c index 203d2a2..49372d2 100644 --- a/include/tray.c +++ b/include/tray.c @@ -45,8 +45,9 @@ int UpdateTray() { // Only add or modify if not hidden or if balloon will be displayed if (!hide || tray.uFlags&NIF_INFO) { - // Try until it succeeds, sleep 100 ms between each attempt - while (Shell_NotifyIcon((tray_added?NIM_MODIFY:NIM_ADD),&tray) == FALSE) { + // Try a few times, sleep 100 ms between each attempt + int i=0; + while (i++ < 3 && Shell_NotifyIcon((tray_added?NIM_MODIFY:NIM_ADD),&tray) == FALSE) { Sleep(100); } // Success From 3dab20fc75b52610c6e2e19e4da335b3f0ff1d7b Mon Sep 17 00:00:00 2001 From: RamonUnch <74856804+RamonUnch@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:36:53 +0200 Subject: [PATCH 2/2] More reliable fix... --- include/tray.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/include/tray.c b/include/tray.c index 49372d2..61f9bc3 100644 --- a/include/tray.c +++ b/include/tray.c @@ -45,9 +45,21 @@ int UpdateTray() { // Only add or modify if not hidden or if balloon will be displayed if (!hide || tray.uFlags&NIF_INFO) { - // Try a few times, sleep 100 ms between each attempt - int i=0; - while (i++ < 3 && Shell_NotifyIcon((tray_added?NIM_MODIFY:NIM_ADD),&tray) == FALSE) { + // Try 3 times, sleep 100 ms between each attempt + int i=1; + while (Shell_NotifyIcon(tray_added? NIM_MODIFY: NIM_ADD, &tray) == FALSE) { + // Maybe we tried to re-add an already existing tray? + // Win10 DPI change cause a WM_TASKBARCREATED message! + // In this case we can try to just update the tray. + if (!tray_added && Shell_NotifyIcon(NIM_MODIFY, &tray)) { + // Sucess + tray_added = 1; + return 0; + } + if (i > 2) { + return 1; // Failde all attempts + } + i++; Sleep(100); } // Success