-1

I get a list of open windows and check if it contains a certain title. It is working but takes more than 10 seconds. Why does it take so long, what is wrong with my code?

Looks like WinList() doesn't list only visible windows.

$title = 0
$begintime = TimerInit()
MsgBox($MB_OK, "Timer", "Timer inicialized")

While $title = 0

    $aList = WinList()

    For $x = 1 To $aList[0][0]

        ;Check if a window with this title exists.
        if $aList[$x][0] = "WindowTitle" Then

            If $lastruntitle = "WindowTitle" Then

                $title = 1
                ExitLoop(2)

            Else

                SendMail4()
                $lastruntitle = "WindowTitle"
                $title = 1
                ExitLoop(2)

            EndIf

        EndIf

    Next

WEnd
user4157124
  • 2,809
  • 13
  • 27
  • 42
Sardar Agabejli
  • 423
  • 8
  • 32
  • 1
    Do you open the necessary window after start of the script or a window already openly at the time of start? If you really need to use `While` loop, you must add some `sleep` as least `sleep(100)`, othertwice cpu will be overloaded. Also show SendMail4() function. – matrix Sep 29 '17 at 16:10
  • "*Why does it take so long, what is wrong with my code?*" Use [`WinExists()`](https://www.autoitscript.com/autoit3/docs/functions/WinExists.htm) or [`WinGetHandle()`](https://www.autoitscript.com/autoit3/docs/functions/WinGetHandle.htm) to check for window's existence. [Related](https://stackoverflow.com/a/50969300/4157124). – user4157124 Jun 21 '18 at 13:08

1 Answers1

1

Simple solution for your task is:

#include <Array.au3>

While 1
   $aList = WinList()
   _ArraySearch($aList, "WindowTitle", 0, 0, 0, 0, 1, 0)
   If Not @error Then
      MsgBox(0,"","Window found!")
      Exit
   EndIf
   Sleep(100)
WEnd
user4157124
  • 2,809
  • 13
  • 27
  • 42
matrix
  • 513
  • 3
  • 8