Jump to content

Prevent running Twice


kpu
 Share

Recommended Posts

I need to figure out how to prevent someone running it twice. What I have is a script that sleeps for "x" amount of time, and then completing a task before closing. Since it doesn't have a window I can't use the one listed at: http://www.autoitscript.com/autoit3/docs/faq.htm#14http://www.autoitscript.com/autoit3/docs/faq.htm#14

I need to check the process. Would it be best to write to a file or registry key, then when it closes it removes it??

Thanks for any help you can give me.

Edited by kpu
Link to comment
Share on other sites

; Place at the top of your script

$g_szVersion = "My Script 1.1"

If WinExists($g_szVersion) Then Exit ; It's already running

AutoItWinSetTitle($g_szVersion)

; Rest of your script goes here

If you run autoit, even in the backround, it does have a window, you just can't see it.

if you use the above script, it will name the hidden autoit script as "My Script 1.1"

If you start the same script (while the first is running), it will find the hidden window with the same name and exit.

You may find later on that you want to have two autoitscripts going at the same time (backup operations, hotkeys to favorite functions, etc.)

if you use processexhist, it will find autoit.exe running every time, because it is an autoit program itself. (you could do a count) If you rename the compiled version you will have to end up changing the code. The script above will not have that problem, if you run a compiled version of the script, a non compiled, or a compiled with a name change, it will just work...

just try out the above code and it will work, trust me.

There is other ways to do it, but this is the easiest path.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

I need to figure out how to prevent someone running it twice. ...Since it doesn't have a window I can't use the one listed at: http://www.autoitscript.com/autoit3/docs/faq.htm#14

Thanks for any help you can give me.

<{POST_SNAPBACK}>

this will work... i use it on mine.. it uses a hidden window that coresponds to your program... it will work if you place it at the TOP of your program... try it first

8)

EDIT:

scriptkitty got there first

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

I've tried:

$g_szVersion = "My Script 1.1"
If WinExists($g_szVersion) Then Exit 
AutoItWinSetTitle($g_szVersion)
Of cource I edited my info in it. :whistle:

The program ran another process. Here's what my program is doing.

1. On run it launches another program.

2. Waits until that program ends by monitoring the process.

3. Once it's closed it asks to make sure it was closed on purpose.

4. Waits 30 min to copy a file and then exit's.

I'll give this one a try next:

If UBound(ProcessList(@ScriptName)) > 2 Then Exit;PREVENT MULTIPLE INSTANCES OF THIS PROGRAM

Thanks for all your help.

Link to comment
Share on other sites

This is a typical script function, and I add it in to a lot of my scripts.

I have a very similar script that opens Access, and starts processing a file. Once that file is completed, it has a certain window open. At that point, I record what that window says, and close access, and make a note in an ini file. I don't want more than one version of the script running, but I don't care if more than one access version is running.

my typical script is:

If WinExists("there can be only one") Then Exit
AutoItWinSetTitle("there can be only one")
msgbox(1,"try and run more of me","not going to happen")
; just an example script.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Here's what I did to get this towork.

1. Created a hot key for F9 the closes the application.

2. Used the following code. It's the only way I got it to work.. ??

If UBound(ProcessList(@ScriptName)) > 2 Then 
    $run2 = Msgbox(164,"Warning","Application is already running. Would you like to run it again?")
    If $run2 = 6 Then
        Send("!{F9}")
        run("c:\fndowntime\program.exe")
        Exit;PREVENT MULTIPLE INSTANCES OF THIS PROGRAM
    Else
    EndIf
EndIf
Link to comment
Share on other sites

Okay, so you got it to work another way, but would you mind telling me how and why the solution mentioned in FAQ#14 would not work for you? You say that your script does not have a window... but the code in the FAQ assigns your script a window title via the AutoItWinSetTitle function... so it must have a window.

Put this into a test script and try to run it two or more times:

$g_szVersion = "My Script 1.1"
If WinExists($g_szVersion) Then
    MsgBox(0,$g_szVersion,"Already running.")
    Exit
EndIf
AutoItWinSetTitle($g_szVersion)
While 1
    Sleep(100)
WEnd

Technically, subsequent attempts at running that code will run, but they should not continue after you answer the msgbox.

...just wondering...

edit: cannnnnot typeeeeee today

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Moderators

I did the UBound a while back, but have since switched to Valiks suggestion of Semiphores.

The @ScriptName if being compiled and sent to another computer causes a situation if the program is removed from said folder.

Here is the entire conversational post for more examples.

Run Once

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You're right, the code does work and it's what I asked for from my orginal post. However, what I needed was to code it so it stops the other process and start a new one. Since the software may need to be ran again in 30 min, I couldn't have it not load. Make sense?? Kinda hard to explain without giving you full detail of what I'm trying to accomplish.

Thank you for your help. Everything is working great now. :whistle:

Link to comment
Share on other sites

Ah, that makes sence.

you could alternately do the same with a slightly modified version, and of course put a question box up as well.

winclose("there can be only one")
AutoItWinSetTitle("there can be only one")

or

$g_szVersion = "My Script 1.1"
If WinExists($g_szVersion) Then
    $run2 = Msgbox(164,"Warning","Application is already running. Would you like to run it again?")
    If $run2 = 6 Then
        Send("!{F9}")
        run("c:\fndowntime\program.exe")
        Exit;PREVENT MULTIPLE INSTANCES OF THIS PROGRAM
    Else
    EndIf
EndIf
AutoItWinSetTitle($g_szVersion)

Anyway, glad you got it working. not sure what the alt-F9 does, but I am sure you have it working just fine. I just wanted to show the code can work equally well to kill the original program, or itself. The process version can be nice if you want to run only X number of processes.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

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...