Sign in to follow this  
Followers 0
The_Noob

Detect if script is already running?

10 posts in this topic

How can i detect if an instance of this script already exists?

i tried

if processexists("script.exe") then exit

but that closes the script that im trying to run. Any suggestions?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

How can i detect if an instance of this script already exists?

i tried

if processexists("script.exe") then exit

but that closes the script that im trying to run. Any suggestions?

_Singleton

Loot it up in the helpfile :)

Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

i read it... its not making sense...

#include <Misc.au3>

if _Singleton("test\test",1) = 0 Then

Msgbox(0,"Warning","An occurence of test is already running")

Exit

EndIf

what is "test/test" ?? i get everything but that...

Suppose my script is named Script.exe... would it be

#include <Misc.au3>
if _Singleton("script.exe",1) = 0 Then
    Msgbox(0,"Warning","An occurence of test is already running")
    Exit
EndIf

Share this post


Link to post
Share on other sites

hmm try the one in the FAQ, its easier to use =P

http://www.autoitscript.com/.../faq.htm#14

From FAQ:

; 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

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

lol that confused me more...

Is there a point for $g_szVersion or could it just be named $version or somthing simple? is there a use for the $g_sz?

And what if its a process without a window? like somthing meant to run in the background? Does that rename the process itself? Whats going on there? I dont just want a "heres the solution, go away" i would much rather know why stuff works so i dont have to keep asking about it later :)

#edit#

So i did a search for _Singleton and found a lot of other people confused with it... it seems like its a long way to do somthing thats otherwise simple.

I also found

Global $AuthAgent="Script"

If WinExists($AuthAgent)=1 Then

Exit

EndIf

AutoItWinSetTitle($AuthAgent)

which is pretty much the same thing as abive, but without the complicated name... so i guess that answers one of my questions.

I decided to give it a go and test it, and its working, which i guess answers all of my other questions concerning MethodZero's post, except why he chose such a long name for $g_szVersion.

My question to PianoMan still stands, but for what its worth this is a lot easier for me to understand, and it seems to work no explinations needed... Why do people need to make things more complicated than they need be? It confuses us noobs! lol

Edited by The_Noob

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

lol that confused me more...

Is there a point for $g_szVersion or could it just be named $version or somthing simple? is there a use for the $g_sz?

And what if its a process without a window? like somthing meant to run in the background? Does that rename the process itself? Whats going on there? I dont just want a "heres the solution, go away" i would much rather know why stuff works so i dont have to keep asking about it later :)

Alright, I think I found the solution.

Put your script name in the function, for example:

#include "Misc.au3"
If Not _Singleton("_Singleton.au3") then Exit  

Msgbox(0, '', 'It works!')

Edit: To test this run it once. Don't push ok on the message box. Then try running it again and see what happens...

Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Alright, I think I found the solution.

Put your script name in the function, for example:

#include "Misc.au3"
If Not _Singleton("_Singleton.au3") then Exit  

Msgbox(0, '', 'It works!')

Edit: To test this run it once. Don't push ok on the message box. Then try running it again and see what happens...

well i know it works... but i want to know what the point of test\test is in the example...

#include <Misc.au3>
if _Singleton("test\test",1) = 0 Then
Msgbox(0,"Warning","An occurence of test is already running")
Exit
EndIf

I tried a few different things in "test/test" and they all worked... im somewhat confused...

the other example works better IMO tho so im gonna stick with that. At this point its just morbid curiousity

Share this post


Link to post
Share on other sites

...Is there a point for $g_szVersion or could it just be named $version or somthing simple? is there a use for the $g_sz?...

It can be named anything you want or not used at all:
; Place at the top of your script
If WinExists("My Script 1.1") Then Exit
AutoItWinSetTitle("My Script 1.1")
; Rest of your script goes here
When you use a variable in code that you ask others to paste into their existing script, you hope to pick a variable name that was not used anywhere else in the script. That said, g_sz probably means something to the author. Ask Holger, he uses "sz" some.

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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

well i know it works... but i want to know what the point of test\test is in the example...

#include <Misc.au3>
if _Singleton("test\test",1) = 0 Then
Msgbox(0,"Warning","An occurence of test is already running")
Exit
EndIf

I tried a few different things in "test/test" and they all worked... im somewhat confused...

the other example works better IMO tho so im gonna stick with that. At this point its just morbid curiousity

Now you've got me confused too... I copied the _Singleton function from the include file and put in 0 where it usually puts the string you put in the first parameter, it still works perfectly.

Correct me if I'm wrong but I don't think the first parameter matters at all. :)

Edit: This is what I did (this is the modified version of _Singleton() that I used)

If _Singleton() Then
    Msgbox(0,"Warning","An occurence of test is already running")
Else 
    Msgbox(0,"OK","the first occurence of test is running")
EndIf

Func _Singleton()
    Local $handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", 0);This last zero was where the string would go
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    If $lastError[0] = 183 Then
        Return True
    Else
        Return False
    EndIf
EndFunc
Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

#10 ·  Posted

It's up to you. Either you use a tested method or you do what your head wants.

The "test\test" example in _Singleton is just an identifier so as to be able to run multiple AutoIT scripts and not confuse the _Singleton function. So if I am right, if you use "test\test" or "blahblah" in all your scripts then running two different scripts that use the same identifier in _Singleton function it will fail. It would be the same as running the same script because of the same identifier.

So change that identifier to whatever you want; just be sure it's something different among different scripts.

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
Sign in to follow this  
Followers 0