Jump to content

Recommended Posts

Posted

I have an AutoIT script that I want to have only one instance of. When I run the script a second time, I want to kill the first one and allow the second one to start up.

At startup, I call a function that looks for another instance and when it finds it, does a WinClose() to kill it.

The problem is that the WinClose() returns a good status, but does not kill the other window. I've tried WinKill() and that does not work either. If I change the code to kill some other window (e.g. Notepad), it kills that window OK.

I suspect that I am not trapping some signal in the running script.

Here is the startup function:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
; runUnique() - See IF another instance of this script is  
;               already running and IF so, kill it. 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func runUnique()
Local $hWnd, $title = $sCurrentTitle ; ($sCurrentTitle is defined elsewhere)

  IF (WinActivate($title, "") <> 0) Then
    $hWnd = WinGetHandle($title, "")
    IF (IsHWnd($hWnd)) Then
      IF (NOT WinClose($hWnd)) Then WinKill($hWnd)
      
      IF (WinActivate($title, "") <> 0) Then
        MsgBox(0, "ERROR", "Cannot kill window")
      Else
        MsgBox(0, "INFO", "Killed other window")
      EndIF
    Else
      MsgBox(0, "ERROR", "Cannot get win handle")
    EndIF
  EndIF
EndFunc   ;==>runUnique

I don't get any errors, but the running script is not killed.

  • Moderators
Posted

AndyS01,

Your code works fine for me. ;)

Does the instance you are trying to close have open files? Is it being accessed by another app? Something of this nature might prevent it closing. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

The app opens an AutoIT source file and gathers a function list, then closes the file. The app then presents the function list in a listbox and when a function is doubleclicked, goes there in the SCite editor (much like what happens when you press ALT-L, but with a separate non-modal window).

I have these hooks to exit my script:

OnAutoItExitRegister("ExitStageLeft_normal")
GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose")

Both of these functions call my general cleanup and exit function. This function saves the window settings to the registry and does an exit(0).

I commented out all of my file I/O stuff and it still fails.

I could send you the script if you want to look at it.

  • Moderators
Posted

AndyS01,

Just post it here. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

OK, after stripping out most of the code, I got it working, but it looks like an AutoIT bug or restriction.

When I included a "Opt("GuiCloseOnESC", 0)" statement in my code, it failed, when I commented it out, it worked.

Here is a stripped down test script that you can use to verify this:

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

[b]Opt("GuiCloseOnESC", 0)[/b]
Global $sCurrentTitle = "Test WinClose()"

runUnique() ; Kill off any currently running version of this script

$hGUI = GUICreate($sCurrentTitle, 300, 200)

$hListView = _GUICtrlListView_Create($hGUI, "Items|SubItems", 2, 2, 296, 196, $LVS_REPORT)

For $i = 1 To 10
    _GUICtrlListView_AddItem($hListView, @HOUR & ":" & @MIN & ":" & @SEC & " - " & $i)
    _GUICtrlListView_AddSubItem($hListView, $i - 1, "SubItem " & $i, 1)
Next

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
exit (1)

Func runUnique()
    Local $hWnd, $res, $title = $sCurrentTitle

    if (WinActivate($title, "") <> 0) Then
        $hWnd = WinGetHandle($title, "")
        If (Not IsHWnd($hWnd)) Then $hWnd = HWnd($hWnd)
        if (IsHWnd($hWnd)) Then
            $res = WinClose($hWnd)
            if ($res == 0) Then
                ConsoleWrite("+++: WinClose() failed")
                $res = WinKill($hWnd)
                ConsoleWrite("+++: WinKill() returned " & $res & @CRLF)
                if ($res == 0) Then
                    ConsoleWrite("+++: WinKill() failed")
                EndIf
            EndIf

            if (WinActivate($title, "") <> 0) Then
                MsgBox(0, "ERROR", "Cannot kill window")
            Else
                MsgBox(0, "INFO", "Killed other window")
            EndIf
        Else
            MsgBox(0, "ERROR", "Cannot get win handle")
        EndIf
    EndIf
EndFunc   ;==>runUnique

Compile the script (F7), then open a DOS window in the same directory and run the program twice. The second time should kill the first instance and start a new one. The behavior is different, depending on whether the "Opt("GuiCloseOnESC", 0)" line is commented in or out.

Do you see this behavior? If so, what's happening?

AndyS01

  • Moderators
Posted

AndyS01,

I can confirm the problem - but I have absolutely no idea why it happens. ;)

Looks like you get to open a new bug ticket in Trac! ;)

But I can close the old window, with Opt("GuiCloseOnESC", 0) active, by using ProcessClose like this:

Func runUnique()
    Local $hWnd, $title = $sCurrentTitle, $iPID

    If WinExists($title, "") Then
        $hWnd = WinGetHandle($title, "")
        _WinAPI_GetWindowThreadProcessId($hWnd, $iPID)
        ProcessClose($iPID)
        If WinExists($title, "") Then
            MsgBox(0, "ERROR", "Cannot kill window")
        Else
            MsgBox(0, "INFO", "Killed other window")
        EndIf
    EndIf
EndFunc   ;==>runUnique

I hope that lets you progress while the "GuiCloseOnESC" matter gets resolved. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

Excellent suggestion!! Now I can use "GuiCloseOnESC" the way I want to. So many ways to skin this cat!!!

BTW: Where do I go to to report this problem?

  • Moderators
Posted

AndyS01,

Bug Reports and Feature Requests

The "New Ticket" button is at top-right. Explain the problem clearly and put in a minimalist reproducer script (I would cut down the one you already have even further). Then wait - they are are bit busy at the moment, so do not hold your breath. :)

Glad you like the other method. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
×
×
  • Create New...