Sign in to follow this  
Followers 0
amidy

Stopping a script from running if its already running.

12 posts in this topic

Hello all!

I'm using a IR based remote to Trigger a script, and start two programs and it tends to trigger the script twice or three times causing one of the apps to crash.

So I'm wondering if there is a If, or a command that i could put in place on t he script to make sure that a previous instance of the script is running for it to close itself.

Thanks in advance!

Share this post


Link to post
Share on other sites



You better compile the script and preform a process check in the header so each instance shuts itself if it's name is already in the returning array of the ProcessList() function.

Share this post


Link to post
Share on other sites

Or just lookup the _Singleton command in the Help file.

Share this post


Link to post
Share on other sites

using this in my script.

;////////////////////////////////////////////////////////////////////
;//   controllo esistenza di una sola sessione - chiudo le altre !
;////////////////////////////////////////////////////////////////////
$list = ProcessList(@ScriptName)
for $i = 1 to $list[0][0]
    if $i > 1 Then
        msgbox(0, "ClipME", "Puoi lanciare una sola sessione di " & @ScriptName, 1)
        ProcessClose($list[$i][1])
    EndIf
next

m.

Share this post


Link to post
Share on other sites

Or just lookup the _Singleton command in the Help file.

This.

Just saying "This." because this is really what you need. Listing other processes and such is not a good alternative.

Share this post


Link to post
Share on other sites

Mutex is another way and the function takes only 3lines :)

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Without function :

$hMutex = DllCall('kernel32.dll', 'hwnd', 'OpenMutex', 'int', 0x1F0001, 'int', False, 'str', @ScriptName)
If $hMutex[0] Then Exit MsgBox(16, 'Warning - Error !', 'Only one instance of ' & @ScriptName & ' is allowed !')
DllCall('kernel32.dll', 'hwnd', 'CreateMutex', 'int', 0, 'int', False, 'str', @ScriptName)oÝ÷ Ùh­û§rب«­¢+Ù%}5ÕÑà¡MÉ¥ÁÑ9µ¤Q¡¸á¥Ð5Í  ½à ÄØ°Ìäí]ɹ¥¹ÌÌìÌäì°Ìäí=¹±ä½¹¥¹Íѹ½ÌäìµÀìMÉ¥ÁÑ9µµÀìÌäì¥Ì±±½ÝÌÌìÌäì¤()Õ¹}5ÕÑà ¤($ÀÌØí¡5ÕÑàô±±
±° Ìäí­É¹°Ìȹ±°Ìäì°Ìäí¡Ý¹Ìäì°Ìäí=Á¹5ÕÑàÌäì°Ìäí¥¹ÐÌäì°ÁàÅÀÀÀÄ°Ìäí¥¹ÐÌä찱ͰÌäíÍÑÈÌäì°MÉ¥ÁÑ9µ¤(%%ÀÌØí¡5ÕÑálÁtQ¡¸IÑÕɸÄ(%±±
±° Ìäí­É¹°Ìȹ±°Ìäì°Ìäí¡Ý¹Ìäì°Ìäí
ÉÑ5ÕÑàÌäì°Ìäí¥¹ÐÌäì°À°Ìäí¥¹ÐÌä찱ͰÌäíÍÑÈÌäì°MÉ¥ÁÑ9µ¤(%IÑÕɸÀ)¹Õ¹ìôôÐí}5ÕÑà

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Nice! Small and easy to use. Might want to add an @error check in there before you try to return the array though, since as I understood it DllCall doesn't return an array when it fails?

By the way, this doesn't require admin privileges does it?

Thanks

*by the way, _Singleton does the same thing with ignoring errors and returning an array element. I thought that was supposed to be fixed per the changes History.. well at least the _WinAPI functions were fixed..

Edited by ascendant

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@ascendant

I dont think its useful to add IsArray function because the DllCall 'always' work except when the dll is different or doesnt exists, but in this case im not sure that the os is working well :)

Cheers, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Hi guys,

_Singleton is not working correctly, is it?

I'm using AutoIt 3.3.0.0 on Windows XP SP3.

Could you try it and let me (us) know if it still works for you?

Try this for example

#include <Misc.au3>
_Singleton("EnterSomeUniqueStringForYourScript1223456blabla")

MsgBox(0,"test","Test - keep this open and try to start the exe again!")
Exit

thanks, cya


You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/

Share this post


Link to post
Share on other sites

Works fine for me. AutoIt v3.3.0.0 Windows XP SP3.

Oh, that's weird. Thanks mate.

Now I know where I'll have to look. Maybe Kaspersky or .Net or so interferes.

I checked the return values and instead of the expected 183 the GetLastError returns 87.

Rasim's Mutex() works just fine though.

Maybe one of the AutoIt Gods around here knows why that happens.

I'll post the solution/problem if I can figure it out. Someone else might have this problem one day or another.

greetz


You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/

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