Jump to content

Query to close previous instance or restore


TheSaint
 Share

Recommended Posts

This is just a little helpful script, that I started using a month or so ago (one program so far), with this version having a Restore ability added (so now using with two programs of mine, as of tonight).

You can find other alternatives or similar scripts here, the most recent being - _SingleScript

guinness also has something much more elaborate, that is well worth checking out - _SingletonHWID.

Just place the following at the beginning of your script, and adjust where needed and wanted (including Scriptname, variable names and MsgBox magic numbers, etc). Perhaps you should read and use the following - Best coding practices ... unlike my slack hobby programmer self (not always).

#include <Misc.au3>

Global $ans, $exe, $handle, $pid, $script, $Scriptname, $status, $w, $wins

; Assign the program name (window title).
$Scriptname = "Update Mp3 Artwork v7.9  (updated December 2015)"

; The following line both sets the unique ID of the current program, plus queries if a program with the same ID is running already.
$status = _Singleton("update-mp3artwork-thsaint", 1)
If $status = 0 Then
    ; A program with the same ID is already running.
    ; Assign a PID for previous script, based on current executable name, whether current script is compiled or not, and assign the executable name.
    If @Compiled = 1 Then
        $pid = ProcessExists(@ScriptName)
        $exe = @ScriptName
    Else
        $pid = ProcessExists("AutoIt3.exe")
        $exe = "AutoIt3.exe"
    EndIf
    ; Assign a PID for current script.
    $script = @AutoItPID
    ;
    $ans = MsgBox(262177, "Close Running Instance Query", _
        "'Update Mp3 Artwork' program is already running." & @LF & @LF & _
        "Do you want to close it for another instance?" & @LF & @LF & _
        "NOTE - If all work has been saved, and you are" & @LF & _
        "trying to work on another album, then a click on" & @LF & _
        "OK is recommended, else just click CANCEL, to" & @LF & _
        "attempt a restore or re-activate of original." & @LF & @LF & _
        $pid & " (" & $exe & ") " & $script & @LF & @LF & _
        "(will default to CANCEL in 30 seconds)", 30)
    If $ans = 1 Then
        ; Attempt to close original instance, and continue with new.
        If $pid <> $script Then
            ProcessClose($pid)
        Else
            MsgBox(262192, "Close Error", "OK process failed!", 0)
            Exit
        EndIf
    ElseIf $ans = 2 Or $ans = -1 Then
        ; Attempt to retore and activate a non-active or minimized window.
        ; Get a list of all running programs with the same name.
        $wins = WinList($Scriptname, "")
        ; Loop through found instances and compare PID's, restoring or re-activating any match.
        For $w = 1 to $wins[0][0]
            $handle = $wins[$w][1]
            If WinGetProcess($handle, "") = $pid Then
                WinSetState($handle, "", @SW_RESTORE)
                WinActivate($handle, "")
                ExitLoop
            EndIf
        Next
        Exit
    EndIf
EndIf

Enjoy!

P.S. I have a couple of programs that I regularly forget are minimized while working with another related one, so this saves me some mucking around to close and restart or restore etc. Pretty basic, but does the job well so far.

Edited by TheSaint

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

Spoiler

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)

userbar.png

Link to comment
Share on other sites

I've added some more comments to first post and script.

The first time I used this code, before the update, I also had a command-line parameter query. I didn't need that in the above version, but you could add it back in as an option, so that if no parameter is detected then a restore happens. perhaps without a query. So likewise, if a parameter is detected, then close original (first) instance and process parameters and new running instance of the program without a query.

If one were to make a UDF based on my code etc, then all these elements/options should probably be considered and included ... plus any more that come to mind (user renaming the program executable, etc).

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

Spoiler

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)

userbar.png

Link to comment
Share on other sites

I'm now using a partial implementation of this code (pre-determined without prompts), with the latest version of my KindEbook Wishlist program (v1.9), for any that want to check some of the code out in action.

That means I am now using three variations with three different programs.

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

Spoiler

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)

userbar.png

Link to comment
Share on other sites

  • 1 month later...

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

×
×
  • Create New...