Jump to content
KF5WGB

Script does not restart

Recommended Posts

KF5WGB

Hi ,

I wrote a setup function in which the user can change IP or Port of a Server. To make the changes known to the program, it needs to be restarted.

But it does not restart. Here is the Function:

Func Setup()

#Region ### START Koda GUI section #
## Form=c:\users\kg5cwz\desktop\autohotkey\qrz uploader\a2q.kxf
$Form1_1 = GUICreate("Station Setup", 615, 328, -1, -1, BitOR($WS_SIZEBOX,$WS_THICKFRAME,$WS_SYSMENU,$WS_POPUP))
GUISetBkColor(0xA0A0A4)
$CS= GUICtrlCreateInput($Callsign, 200, 120, 201, 29)
GUICtrlSetFont(-1, 16, 400, 0, "@GungsuhChe")
$Key = GUICtrlCreateInput($QRZKey, 200, 167, 233, 29)
GUICtrlSetFont(-1, 16, 400, 0, "@GungsuhChe")
$IPAddress = GUICtrlCreateInput($ServerIP, 200, 214, 129, 29)
GUICtrlSetFont(-1, 16, 400, 0, "@GungsuhChe")
$UPort = GUICtrlCreateInput($Port, 200, 264, 57, 29)
GUICtrlSetFont(-1, 16, 400, 0, "@GungsuhChe")
$Cancel = GUICtrlCreateButton("Cancel", 384, 272, 81, 25)
$Save = GUICtrlCreateButton("Save", 504, 272, 81, 25)
$INFO = GUICtrlCreateLabel("A2Q Station Setup", 88, 32, 429, 51)
GUICtrlSetFont(-1, 35, 800, 0, "@GungsuhChe")
$Icon1 = GUICtrlCreateIcon(".\1475471331_marine_radio.ico", -1, 464, 112, 129, 113)
$Label1 = GUICtrlCreateLabel("Callsign:", 24, 120, 130, 31)
GUICtrlSetFont(-1, 20, 400, 0, "@GungsuhChe")
$Label2 = GUICtrlCreateLabel("QRZ Key:", 25, 166, 116, 31)
GUICtrlSetFont(-1, 20, 400, 0, "@GungsuhChe")
$Label3 = GUICtrlCreateLabel("IP Address:", 24, 215, 158, 31)
GUICtrlSetFont(-1, 20, 400, 0, "@GungsuhChe")
$Label4 = GUICtrlCreateLabel("Port:", 26, 261, 74, 31)
GUICtrlSetFont(-1, 20, 400, 0, "@GungsuhChe")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE, $Cancel
            GUIDelete($Form1_1)
            ExitLoop
         Case $Save
            Global $hFileOpen = FileOpen("Setup.txt", 2)
               $Callsign = StringUpper(GUICtrlRead($CS))
               $QRZKey= StringUpper(GUICtrlRead($Key))
               $ServerIP = GUICtrlRead($IPAddress)
               $Port = GUICtrlRead($Uport)
            FileWrite($hFileOpen, StringUpper($Callsign) & @CRLF)
            FileWrite($hFileOpen, StringUpper($QRZKey) & @CRLF)
            FileWrite($hFileOpen, $ServerIP & @CRLF)
            FileWrite($hFileopen, $Port)
            FileClose($hFileOpen) ; End write Setup file

         GUIDelete($Form1_1)
            
        If @Compiled Then
        Run("A2Q.exe" & "/RESTART");just run the exe
        Else
        $AutoIt3Path = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "betaInstallDir");installDir for beta
        $AutoIt3Path = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir");installDir for production
        $ToRun1 = '"' & $AutoIt3Path & '\AutoIt3.exe "' & ' "' & @ScriptFullPath & '"'
        ConsoleWrite($ToRun1 & @CRLF);to test we have got it right
        Run($ToRun1)
        EndIf
    EndSwitch
 WEnd
 EndFunc

Func Help()
   MsgBox(64,"Help","Press:" &@CRLF & @CRLF &"ALT-Shift-S = Setup." & @CRLF & "Shift-ESC = Exit")
EndFunc

A2Qis an .EXE. For now I have to manually exit it and then do a restart. I also tried RUN(".\A2Q.exe" & " /RESTART") and the @filepath thingy.

I can not get the script to restart itself :-(

Any ideas? Thanks for any help.

KF5WGB

 

 

 

 

Edited by KF5WGB
typo

Share this post


Link to post
Share on other sites
MattHiggs

I have done something similar in the past.  What I did was I created a separate exe (or a3x) file that I included in the original script that would act as the "relauncher".  Then, I created the following function in main script:

Func refresh()
    If FileExists($path & "\hold.txt") Then
        FileDelete($path & "\hold.txt")
    EndIf
    If @exitCode = 3 Then

        $handle = FileOpen($path & "\hold.txt", $FO_OVERWRITE)
        FileWrite($handle, FileGetShortName(@ScriptFullPath) & "1")
        FileClose($handle)
        ShellExecute("relaunch.a3x", "", $path)
    ElseIf @exitCode = 2 Then
        $handle = FileOpen($path & "\hold.txt", $FO_OVERWRITE)
        FileWrite($handle, FileGetShortName(@ScriptDir & "\passwnew.exe") & "2")
        FileClose($handle)
        ShellExecute("relaunch.a3x", "", $path)
        _SelfDelete()
    Else


        ;$procc = Run ( $path & "\relaunch.bat", "", @SW_SHOW ) ;$STDIN_CHILD )
        ;StdinWrite ( $procc, @ScriptFullPath )
    EndIf

EndFunc   ;==>refresh

I also included OnAutoItExitRegister("refresh") at the beginning.  This way, whenever you call "exit" with a parameter of 3, it will write the location of the main executable to a txt file, call the relauncher, which will read the script location from the txt file, and launch it.  Relaunch script:

Sleep (1000)
$again = FileOpen ( @AppDataDir & "\filler\hold.txt" )
FileSetPos ( $again, 0, $FILE_BEGIN )
$loc = FileRead ( $again )
FileClose ( $again )
If StringRight ( $loc, 1 ) == "1" Then
    $loc = StringTrimRight ( $loc, 1 )
Run ( @ComSpec & " /c " & $loc, "", @SW_HIDE )
ElseIf StringRight ( $loc, 1 ) == "2" Then
    $loc = StringTrimRight ( $loc, 1 )
    $dss = StringReplace ( $loc, "passwnew.exe", "passw.exe" )
    FileMove ( $loc, $dss )
    Run ( @ComSpec & " /c " & $dss, "", @SW_HIDE )
Else
EndIf

 

Share this post


Link to post
Share on other sites
MattHiggs
6 hours ago, AutoBert said:

This

Run(@AutoItExe & " /RESTART");just run the exe

should work.

Interesting.  Learn something new every day.  didn't know AutoIT executables had a parameter like that.  In the Event "Run" doesn't work, try this:

ShellExecute ( @AutoItExe, "/Restart", @ScriptDir )

 

Share this post


Link to post
Share on other sites
KF5WGB
17 hours ago, AutoBert said:

This

Run(@AutoItExe & " /RESTART");just run the exe

should work.

..and it did. Thanks AutoBert. Just wondering why

Run("A2Q.exe" & "/RESTART")

did not work. Is it because of "missing" directory info? Anyway...it works now.

Share this post


Link to post
Share on other sites
AutoBert

The space before parameter is needed. Full path ist only needed if workingdir was changed (FileopenDialog etc.)

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

  • Similar Content

    • Aleksandar
      By Aleksandar
      So can anyone help me ? I need to restart program when condition is not met ...

    • krasnoshtan
      By krasnoshtan
      Hello forum users!
      At some point I had a need to restart my script. Is it possible to do this with autoit or do I need to connect additional scripts like .cmd or .bat?
      For example: i have only script.au3 and it's running. Wich code shoud i use to exit from the script and start it again?
    • DrAhmed
      By DrAhmed
      hey
      I wonder if there is a possibility to do this in Autoit ? I've to make sure that my application is closed properly to prevent crahses / errors
      in VB.NET I was able to do it by :
                      AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf myfunctions_end_mywork
       
      I found an old example here : but its not working 
       
       
    • PEscobar
      By PEscobar
      So I made a loop beginning with while $e = 1. It opens a file and searches for a colour in the file, if the colour isn't there $e = 2 so the loop ends and restarts. Instead of that, the loop runs once and then just stops completely, even if the colour is there. 
      Here is what it looks like basically.
      While $e = 1 
          OpenPng()
            $aCoord = PixelSearch ($left, $top, $right, $bottom, 0x3B5E05, 0)
            If @error Then $e = 2
      WEnd
      How do I make it so the loop restarts if the colour isn't there, but it continues normally if the colour is there?
       
       
    • NiceBoy1234
      By NiceBoy1234
      I want to restart a program, if the window or process closes, the program gets crashed and so on.
      I used Runwait in a While loop, that worked so far really good.
      Now I got the problem that Windows sometimes does show me this window
      http://i.stack.imgur.com/4bCtC.png
      it is not excatly that one but looks close to it.

      So I want to close that window immidiatly if it appears.
×