Help please with a temporary condition.

In the cycle, there is a check that the window is in focus, it is necessary as soon as the window focus is lost within 5 seconds to idle the cycle (to skip conditions), after 5 seconds the normal operation of the cycle again.

until did so:

IntPtr hWnd = FindWindow(null, "Название окна"); while (true) { //если hWnd нужного окна и текущего активного не совпадают, значит фокус пропал if (hWnd != GetForegroundWindow()) { MessageBox.Show("тут нужная функция"); } else continue; } 

but this is not at all what is needed, I can’t figure out how to do it.

  • what a joke? to hang a listener is no longer mainstream? - Gorets
  • @Merlin: does the window belong to your program or not? If yes, Winforms or WPF? Tell us better what you want to achieve at a higher level. - VladD
  • The window is not mine. At a higher level: as soon as the focus of the window is lost for 5 seconds, the cycle is idle (skip conditions), after 5 seconds the normal operation of the cycle again. The condition in the cycle no matter what. - Merlin

1 answer 1

  • The correct Google request is "Hook WM_ACTIVATE или WM_SETFOCUS / WM_KILLFOCUS" .

  • Everything else, including, naturally "while (1) GetForegroundWindow" is a bad idea.

  • Incidentally, it is possible that the foreground window is not the droid you are looking for. I really don't know your real task.