Jump to content
Sign in to follow this  

Recommended Posts

Hi all, I am writing some scripts to control some lab equipment, and sometimes I observe that the script "misses" one of the steps, leading to cascading errors. For example, at one part of the code I wrote this:


Most of the times this works, but I found that in some occasions the windows fail to close. Then, the code becomes a mess, because a second instance of the program will launch, and both windows now have the same title. I think I can solve this problem with this code:

While WinExists("Hype")

I would like to know if anybody has any other suggestions for this kind of redundant code. Thanks,





Share this post

Link to post
Share on other sites

If the window fails to close "instantly", then you could use WinWaitClose with a larger timeout, in order to don't have another instance of the window you are trying to close :)

Click here to see my signature:



  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.




Share this post

Link to post
Share on other sites

To more accurately target a specific window, perhaps look at getting the Handle and maybe even using the PID.

You may also be able to close the main process, to make sure the window(s) close.

Testing the status of the window to allow exiting the loop, beyond what you have done, could also be used ... as in have an alternate response upon repeated failure.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.


What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jantograaf
      Hi all,
      I'm having some trouble restoring a program which uses a normal AutoIT-GUI and is used as a ToolKit for our customers to open different (external) tools, check their network connections or ping different destinations. Almost all customers are running this script on Win7 x64. 
      What do I want to do?
      When the program is already running and the user tries to start it for a second time, it should exit the second instance after activating/restoring the first instance.
      What did I do to integrate this?
      I used a little piece of script found on the forums to prevent opening a second instance and modified it as follows:
      $g_szVersion = "VSOL ToolKit" If WinExists($g_szVersion) Then Local $gState = WinGetState($g_szVersion) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_SHOW) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_RESTORE) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinActivate($g_szVersion) Exit EndIf AutoItWinSetTitle($g_szVersion) I have tried it with only the @SW_SHOW, only the @SW_RESTORE, both of them in a different order, same difference, it doesn't work.
      What does happen then?
      It detects the current state (when it's minimized) as '5', which means enabled and hidden. After setting the state to show, the state changes to 7. Restoring doesn't change the state. But what is really odd, is that the window doesn't appear but AutoIt seems to create a néw window with the same title as the existing one. 

      It doesn't matter if the window was originally minimized by clicking the minimize button in the GUI or by a line of code in my script. For example, when users open a software package from within my script, a similar function to this one will be called:
      Func VSOL_FFTOOLS_SHOW_DOWNLOADS() GUISetState(@SW_MINIMIZE,$VSOL_BACKEND) ShellExecute(@UserProfileDir & "\Downloads") EndFunc The only thing that does work, is when the window is running in a non-minimized state (somewhere behind the current window), it gets activated and receives the focus when trying to start the second instance.
      If anyone could help me out with this one, I'd be forever thankful :-)
      Thanks in advance!
      Kind regards,
      Jan Geurts
    • By afallenhope
      Hello all I am trying to use this function to detect if a specific window is running. Then for it to activate it and send an ALT+F command to close the window. But I cannot seem to get it to detect the window and subsequently the rest of the code does not work. 
      #include <MsgBoxConstants.au3> Handleit() Func Handleit() If WinExists("[CLASS:MsiDialogCloseClass]","InstallShield") Then WinActivate("[CLASS:MsiDialogCloseClass]","Installshield") Send("!f") Else MsgBox($MB_SYSTEMMODAL, "", "Uhhh... something didn't work. Fix it!") EndIf EndFunc Is there anything that I can substitute for the CLASS? 
      Thank you for taking the time to read my question! 
    • By NiceBoy1234
      I want to check if the cmd.exe + window of it is running, if it is running I want to put the window to the foreground, but somehow this does not work:
      Local $sCommandlinePath = "C:\Windows\system32\cmd.exe" RunCm() Func RunCm() If FileExists($sCommandlinePath) Then If ProcessExists("cmd.exe") Then If WinExists("[CLASS:ConsoleWindowClass]") And WinActive("[CLASS:ConsoleWindowClass]") Then Local $hCmdWindow = WinGetHandle("[CLASS:ConsoleWindowClass]") WinSetOnTop($hCmdWindow, "", $WINDOWS_ONTOP) EndIf Else Local $iCmMax = Run($sCommandlinePath, "", @SW_SHOWMAXIMIZED) EndIf EndIf EndFunc I started the command line and put another window above it then I started my script and it did not put the command line window to the foreground.
    • By kcvinu
      Hi all,
      I did some experiments with FileOpenDialog function. I did this from a GUI's button click. Then i wrote another function to the same script to find the file open dialog window with WinExists function. But i can't detect the presence of the open dialog with "WinExists" function. I think when the FileOpenDialog is came, then the main script is not focused. Then i tested it with another script. That is - I run the first function (the one which contains the gui code and the fileOpenDialog code) from another IDE. And i run th window detecting function (the WinExists function) from SciTE. Then it worked. So my qustion is- Is it possible to do this task from the same script ?
      This is the code that not working
      #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> $hGUI = GUICreate("Window", 500, 400, -1, -1) Global $hButton = GUICtrlCreateButton("Button", 144, 179, 202, 60) GUISetState() While 1 Sleep(30) $hMsg = GUIGetMsg() Switch $hMsg Case $GUI_EVENT_CLOSE Exit Case $hButton BtnClick() EndSwitch Checker() WEnd Func BtnClick() Local $sPath = FileOpenDialog("TestWindow","D:\AutoIt Works\EXEs","(*.*)") ConsoleWrite($sPath & @LF) EndFunc Func Checker() if WinExists("TestWindow") = 1 Then ConsoleWrite("Yes, it is there" & @CRLF) EndIf EndFunc After first failure, i ran the Checker function from SciTE and this gui function from another IDE. Then it worked. 
    • By grimmlock
      I am looking for some help with my code. I want to be able to have this script always run, and append a file each time a program is opened adding a new line. Here is my code, and help would be greatly appreciated.
      #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <File.au3> Local $aLines, $iCount = 0 local $hDate = @YEAR & '-' & @MON & '-' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC local $hfile=FileOpen("C:\Users\public\Desktop\new1.txt", $FO_APPEND) Local $hComputername = @ComputerName ; Computer Name Local $hUsername = @UserName ; User Name While 1     If $hfile = -1 Then         SplashTextOn("Error", "Unable to open and append the log file.", 300, 50)         Sleep(2000)         SplashOff()     EndIf         _FileReadToArray("C:\Users\public\Desktop\new1.txt", $aLines)         If Not @error Then             ; if the file exists look for the ID starting on the last line             For $i = $aLines[0] To 1 Step -1                 If StringLeft($aLines[$i], 2) == "id" Then             ; get the ID number                     $iCount = Int(StringRegExpReplace($aLines[$i], "id(\d*):.*", "$1"))                     ExitLoop                 EndIf             Next             ; Check we found an ID             If $i = 0 Then                 MsgBox(0, "Error", "No line id found")                 Exit             EndIf         EndIf     If WinExists("[Class:Notepad]", "") Then             FileWrite($hfile, _LineID() & '|' & $hDate & '|' & $hComputername & '|' & "Notepad was opened by " & $hUsername & @CRLF)             Sleep(10)             FileClose($hfile)             Sleep(1000)     EndIf WEnd Func _LineID()     $iCount += 1     Return "id" & StringFormat("%04i", $iCount) & ": " EndFunc ;==> LineID Thanks!
  • Create New...