Jump to content

Autoit Locking Handle


Recommended Posts

Func SetVolume($iVolume)
    CloseSounds()
    $fVolume = $iVolume / 100
    $hVolumeFile = FileOpen("Volume.bat", 2)
    For $i = 1 To 5
        FileWriteLine($hVolumeFile, '"' & @ScriptDir & '\sox\sox.exe" "' & @ScriptDir & '\Sound\Beep-0' & $i & '.wav" "' & @ScriptDir & '\Sound\tempBeep-0' & $i & '.wav" vol ' & $fVolume)
    Next
    FileClose($hVolumeFile)
    While Not(FileDelete(@ScriptDir&"\Sound\tempBeep-01.wav"))
        Sleep(50)
    WEnd
    While Not(FileDelete(@ScriptDir&"\Sound\tempBeep-02.wav"))
        Sleep(50)
    WEnd
    While Not(FileDelete(@ScriptDir&"\Sound\tempBeep-03.wav"))
        Sleep(50)
    WEnd
    While Not(FileDelete(@ScriptDir&"\Sound\tempBeep-04.wav"))
        Sleep(50)
    WEnd
    While Not(FileDelete(@ScriptDir&"\Sound\tempBeep-05.wav"))
        Sleep(50)
    WEnd
    $hPid = Run("Volume.bat", @ScriptDir, @SW_HIDE)
    While ProcessExists($hPid)
        Sleep(10)
    WEnd
    OpenSounds()
EndFunc

Func OpenSounds()
    Global $aSndStart = _SoundOpen(@ScriptDir&"\Sound\tempBeep-01.wav")
    Global $aSndStop = _SoundOpen(@ScriptDir&"\Sound\tempBeep-02.wav")
    Global $aSndHotkey = _SoundOpen(@ScriptDir&"\Sound\tempBeep-03.wav")
    Global $aSndError = _SoundOpen(@ScriptDir&"\Sound\tempBeep-04.wav")
    Global $aSndHotkeyToggle = _SoundOpen(@ScriptDir&"\Sound\Beep-05.wav")
EndFunc

Func CloseSounds()
    _SoundClose($aSndStart)
    _SoundClose($aSndStop)
    _SoundClose($aSndHotkey)
    _SoundClose($aSndError)
    _SoundClose($aSndHotkeyToggle)
EndFunc

I'm changing the volume of some audio files using a command line audio editor called SoX.

Occasionally, even though I closed all the sound handles, Autoit still has a locking handle on the sound files. Hence, it gets stuck in the loops.

Link to comment
Share on other sites

It would seem to me that your While NOT statements are holding up the script. Those statements read to me as "while the file is not deleted, sleep 50ms and keep waiting until it is deleted". Plus, another thing is you are unnecessarily writing commands to a batch file and then running the batch file. You can just run those commands directly from AutoIt and loop them to run all five (or however many you want to run). 

What happens if you run this?

Func SetVolume($iVolume)
    CloseSounds()
    $fVolume = $iVolume / 100
    For $i = 1 To 5
        RunWait('"' & @ScriptDir & '\sox\sox.exe" "' & @ScriptDir & '\Sound\Beep-0' & $i & '.wav" "' & @ScriptDir & '\Sound\tempBeep-0' & $i & '.wav" vol ' & $fVolume) ; runwait eliminates the waiting for the PID to close
    Next
    OpenSounds()
EndFunc
Link to comment
Share on other sites

The problem still occurs with the new code but at least it's a little more efficient now. In fact I did that batch file method in other places in my script so this is great.  :)

The thing about it getting stuck in the while statement is wrong because the statement is executed in each loop.

Func AddOneUntil100(ByRef $iNumber)
    $iNumber += 1
    If $iNumber = 100 Then
        Return True
    Else
        Return False
    EndIf
EndFunc

$iNumber = 0

While Not(AddOneUntil100($iNumber))
    ConsoleWrite($iNumber & @CRLF)
WEnd

ConsoleWrite("$iNumber = " & $iNumber & "!" & @CRLF)

Also I've come to realise that there's something else in my script causing autoit keeping a locking handle on the file. The weird thing is the exact code works if you delete the temp files before running the script but then breaks again when you close and open the script without deleting the files. From this I deduced that something being executed during the startup of the script was the cause of the locking handle.

The OpenSounds() function is called quite early on in the script. I played around with its positioning and found that if the _OnAutoItErrorRegister command wasn't executed before the OpenSounds() function, the locking handle would stay on the sounds.

_OnAutoItErrorRegister("CriticalError")

Func _OnAutoItErrorRegister($sFunction = "", $vParams = "", $sTitleMsg = -1, $sErrorMsgFormat = -1, $bUseStdOutMethod = True)
    If StringInStr($CmdLineRaw, "/OAER") Then
        ;Opt("TrayIconHide", 0)
        Return
    Else
        Opt("TrayIconHide", 1)
    EndIf

    Local $sErrorMsg = "", $iPID, $sRunLine

    If $bUseStdOutMethod Then
        $sRunLine = @AutoItExe & ' /ErrorStdOut /AutoIt3ExecuteScript "' & @ScriptFullPath & '" ' & $CmdLineRaw & ' /OAER'
        $iPID = Run($sRunLine, @ScriptDir, 0, 2 + 4)
        ProcessWait($iPID)

        While 1
            $sErrorMsg &= StdoutRead($iPID)

            If @error Then
                ExitLoop
            EndIf

            Sleep(10)
        WEnd
    Else
        $sRunLine = @AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" ' & $CmdLineRaw & ' /OAER'
        $iPID = Run($sRunLine, @ScriptDir)
        ProcessWait($iPID)

        Opt("WinWaitDelay", 0)

        While ProcessExists($iPID)
            Sleep(10)

            If WinExists("[CLASS:#32770;TITLE:AutoIt Error]") Then
                ExitLoop
            EndIf
        WEnd

        WinWait("[CLASS:#32770;TITLE:AutoIt Error]", "", 3)

        If Not WinExists("[CLASS:#32770;TITLE:AutoIt Error]") Then
            Exit
        EndIf

        $sErrorMsg = ControlGetText("[CLASS:#32770;TITLE:AutoIt Error]", "", "Static2")
        WinClose("[CLASS:#32770;TITLE:AutoIt Error]")
    EndIf

    If $sErrorMsg = "" Then
        Exit
    EndIf

    Call($sFunction, $sErrorMsg)

    Exit
EndFunc

Func CriticalError($sErrorMsg)
    Global $Version

    #region ReportSend
    Global $ReportSend = GUICreate("Something Went Wrong", 490, 186, @DesktopWidth / 2 - 490 / 2, @DesktopHeight / 2 - 186 / 2)
    Global $SendReportButton = GUICtrlCreateButton("Send Error Report", 136, 128, 225, 49)
    GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
    Global $SorryLabel = GUICtrlCreateLabel("The program has crashed.", 8, 8, 187, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    Global $ExcusesLabel = GUICtrlCreateLabel("This could be due to your computer settings or a general programming bug.", 8, 40, 478, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    Global $HopeLabel = GUICtrlCreateLabel("Click 'Send Error Report' to send me anonymous information regarding the crash.", 8, 64, 481, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    Global $WhyLabel = GUICtrlCreateLabel("If you know more information regarding the crash, contact me directly on my site.", 8, 88, 472, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    #endregion ReportSend

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg

            Case $GUI_EVENT_CLOSE
                Exit

            Case $SendReportButton
                GUISetState(@SW_HIDE)
                _Crypt_Startup()
                $bAlgorithm = $CALG_AES_256
                $hKey = "Ley"
                $sErrorMsg &= @CRLF _
                & "System Details:" & @CRLF _
                & "OS Type: " & @OSType & @CRLF _
                & "OS Arch: " & @OSArch & @CRLF _
                & "OS Language: " & @OSLang & @CRLF _
                & "OS Version: " & @OSVersion & @CRLF _
                & "OS Build: " & @OSBuild & @CRLF _
                & "OS Service Pack: " & @OSServicePack & @CRLF _
                & @CRLF _
                & "Script Details:" & @CRLF _
                & "Version: " & $Version & @CRLF _
                & "Directory: " & @ScriptFullPath & @CRLF _
                & @CRLF
                $bEncrypted = Hex(_Crypt_EncryptData($sErrorMsg, $hKey, $bAlgorithm))
                _Crypt_Shutdown()
                $hErrorFile = FileOpen("ErrorReport.txt", 2+16)
                FileWrite($hErrorFile, $bEncrypted)
                FileClose($hErrorFile)
                $hFTPSession = _FTP_Open("Program Report")
                $hFTPServer = _FTP_Connect($hFTPSession, "Server", "Username", "Password")
                _FTP_FilePut($hFTPServer, "ErrorReport.txt", "ErrorReport("&@MON&"-"&@MDAY&"-"&@YEAR&")("&@HOUR&"-"&@MIN&"-"&@SEC&"-"&@MSEC&")"&Random(1, 9999999999, 1)&".txt")
                _FTP_Close($hFTPServer)
                _FTP_Close($hFTPSession)
                Exit

        EndSwitch
    WEnd
EndFunc
Edited by T0M50N
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...