I can not do a check of an open window in a loop.

IntPtr ptr = WinAPI.FindWindow(null, "VKMusic4"); while (ptr == IntPtr.Zero) { if (ptr == IntPtr.Zero) // скорее всего это лишнее) { Console.WriteLine("окно не найдено"); // Если окно не нашли то возвращаемся к поиску. } else { Если нашли то делаем что либо: Console.WriteLine("окно найдено"); } Console.Read(); } 

And then if the window is closed, we do the check again, and so on forever.

  • Install the hook. There is a hook procedure in which the notification of the creation of any top window comes. - nick_n_a
  • C hooks worked very little, is it possible in more detail? - GooliveR
  • The simpler option is to check the validity of the window so IsWindow(ptr) msdn.microsoft.com/ru-ru/library/windows/desktop/ ... As soon as the window is gone - it will return false - nick_n_a
  • How does ptr change in your cycle? - VladD

1 answer 1

I read the question more carefully. Then so:

 while (true) { IntPtr ptr = WinAPI.FindWindow(null, "VKMusic4"); if (ptr == IntPtr.Zero) { Console.WriteLine("окно не найдено"); Thread.Sleep(1000); } else { Console.WriteLine("окно найдено"); do Thread.Sleep(1000); while (WinAPI.FindWindow(null, "VKMusic4") == ptr); } } 

or so:

 IntPtr ptr, old = IntPtr.Zero while (true) { ptr = WinAPI.FindWindow(null, "VKMusic4"); if (ptr == IntPtr.Zero) { Console.WriteLine("окно не найдено"); } else if (ptr == old) { Console.WriteLine("окно уже обработано"); } else { Console.WriteLine("окно найдено"); old = ptr; } Thread.Sleep(1000); } 

Rearrange the lines according to the numbers (X - delete). Line 3 is divided into 0 and 3 - in 0 declaration of the variable, in 3 - assignment.

 3 IntPtr ptr = WinAPI.FindWindow(null, "VKMusic4"); 1 while (ptr == IntPtr.Zero) 2 { 4 if (ptr == IntPtr.Zero) // скорее всего это лишнее) X { 5 Console.WriteLine("окно не найдено"); X // Если окно не нашли то возвращаемся к поиску. X } X else X { 7 Если нашли то делаем что либо: 8 Console.WriteLine("окно найдено"); X } 5 Console.Read(); 6 } 

It should turn out like this:

 IntPtr ptr; while ((ptr = WinAPI.FindWindow(null, "VKMusic4")) == IntPtr.Zero) { Console.WriteLine("окно не найдено"); Console.Read(); } Console.WriteLine("окно найдено"); 
  • If the window is open, then the console closes, if there is no window, the console is hanging, but if there is a window, do I need to do something else ?! after if (ptr != IntPtr.Zero) break; - GooliveR
  • Maybe you need to put Sleep so as not to hang the system in a loop, but the message was not found - do not output? And before Console.Read you need to check whether the console buffer is full. - nick_n_a
  • @ArteS, the answer is updated. I inattentively read the question. - Qwertiy
  • @Qwertiy, This is already something) But the cycle goes only if the window is not found, if found, if the window is closed again, how to continue the window search cycle? - GooliveR
  • @ArteS, uh ... the endless loop goes on ... endlessly. And at each iteration, looking for a window. What is wrong with closing? - Qwertiy