Sign in to follow this  
Followers 0
j0kky

Avoiding multiple application starts

11 posts in this topic

#1 ·  Posted (edited)

Hi!

I wrote some lines of code to avoid multiple contemporaneous application starts but I think there's an easier way to do that:

#include

$My_handle = GUICreate("Window", 425, 164, 192, 124)
$button = GUICtrlCreateButton("text1", 24, 16, 161, 49)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetOnEvent(-13, "_ripristina_finestra")

Global $file_ini = @TempDir & "123myfileini.ini"
Local $var_esiste = FileExists($file_ini), $dati_ini

if $var_esiste = 0 Then
Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID], ["Handle", $My_handle]]
IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)

Else
$dati_ini = IniReadSection($file_ini, "Sezione1") ;memorizza le chiavi del file ini in un array
if @error Then
MsgBox(16, "Errore", "Si è verificato un errore nell'apertura del file .ini")
FileDelete($file_ini)
Exit
EndIf

Local $var_nome_processo = $dati_ini[1][1], $var_PID = $dati_ini[2][1], $var_handle = HWnd($dati_ini[3][1])
Local $lista_processi = ProcessList($var_nome_processo)
if $lista_processi[0][0] > 0 Then
For $i = 1 to $lista_processi[0][0]
if $lista_processi[$i][1] = $var_PID Then
_ripristina_finestra($var_handle)
Exit
EndIf
Next
EndIf

FileDelete($file_ini)
Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID], ["Handle", $My_handle]]
IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)
EndIf

GUISetState()

Func _ripristina_finestra($handle_finestra)
if Not IsDeclared("handle_finestra") Then $handle_finestra = $My_handle
Local $state = WinGetState($handle_finestra)

;If BitAND($state, 16) Then $controllo = GUISetState(@SW_RESTORE, $handle_finestra)
If BitAND($state, 16) Then WinSetState($handle_finestra, "",@SW_RESTORE)
;If Not BitAND($state, 2) Then GUISetState(@SW_SHOW, $handle_finestra)
If Not BitAND($state, 2) Then WinSetState($handle_finestra, "",@SW_SHOW)
EndFunc

while 1
$a = GUIGetMsg()
Switch $a
Case $GUI_EVENT_CLOSE
Exit
Case $button
GUICtrlSetData($button, "text2")
While 1
$nMsg2 = GUIGetMsg()
Switch $nMsg2
Case $button
ExitLoop
Case $GUI_EVENT_MINIMIZE
GUISetState(@SW_HIDE)
EndSwitch
WEnd
EndSwitch
WEnd

It creates an .ini file in %temp% folder where it puts its application datas (Name, PID and GUI handle).

The new-open checks if exists a process with the same name (and the same PID) and if found it restores the previous window and closes itself.

But I've 3 questions:

- Does GuiSetState work only with local handle (and not with external application handle)? Because the script works only with WinSetState...

- Why do the new script interrupt the previous script's loop when it restore the window?

- Is there an easier way to check if the script is already opened?

P.S: obviously the script works only if it's compiled.

P.P.S: sorry for my bad english, I'm italian :rolleyes:

Edited by j0kky

Share this post


Link to post
Share on other sites



Look at the _Singleton function, this will allow you to set your program to only run once and will block any further launches of it.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

You're not minimizing it, you're hiding it. Double clicking on the AutoIt icon in the system tray doesn't cause your program to unhide, it pauses the script. Look in the help file for Tray Management to figure out how to do what you want.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

But these lines avoid script pause when double-clicking on tray!

Opt("TrayMenuMode", 1)

Opt("TrayOnEventMode", 1)

TraySetOnEvent(-13, "_ripristina_finestra")

My problem is that if I click on "Text1" button and then I minimize the window (so the script hides itself), when I doble-click on the tray, the windows shows itself but the loop seems be partially interrupted ("text1" has no effect when clicked):

#include [color=#282828][font=helvetica, arial, sans-serif]<GUIConstantsEx.au3> [/font][/color]

$My_handle = GUICreate("Window", 425, 164, 192, 124)
$button = GUICtrlCreateButton("text1", 24, 16, 161, 49)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetOnEvent(-13, "_ripristina_finestra")

Global $file_ini = @TempDir & "123myfileini.ini"
Local $var_esiste = FileExists($file_ini), $dati_ini

if $var_esiste = 0 Then
Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID], ["Handle", $My_handle]]
IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)

Else
$dati_ini = IniReadSection($file_ini, "Sezione1") ;memorizza le chiavi del file ini in un array
if @error Then
MsgBox(16, "Errore", "Si è verificato un errore nell'apertura del file .ini")
FileDelete($file_ini)
Exit
EndIf

Local $var_nome_processo = $dati_ini[1][1], $var_PID = $dati_ini[2][1], $var_handle = HWnd($dati_ini[3][1])
Local $lista_processi = ProcessList($var_nome_processo)
if $lista_processi[0][0] > 0 Then
For $i = 1 to $lista_processi[0][0]
if $lista_processi[$i][1] = $var_PID Then
_ripristina_finestra($var_handle)
Exit
EndIf
Next
EndIf

FileDelete($file_ini)
Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID], ["Handle", $My_handle]]
IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)
EndIf

GUISetState()

Func _ripristina_finestra($handle_finestra)
if Not IsDeclared("handle_finestra") Then $handle_finestra = $My_handle
Local $state = WinGetState($handle_finestra)

;If BitAND($state, 16) Then $controllo = GUISetState(@SW_RESTORE, $handle_finestra)
If BitAND($state, 16) Then WinSetState($handle_finestra, "",@SW_RESTORE)
;If Not BitAND($state, 2) Then GUISetState(@SW_SHOW, $handle_finestra)
If Not BitAND($state, 2) Then WinSetState($handle_finestra, "",@SW_SHOW)
EndFunc

while 1
$a = GUIGetMsg()
Switch $a
Case $GUI_EVENT_CLOSE
Exit
Case $button
While 1
$nMsg2 = GUIGetMsg()
Switch $nMsg2
Case $button
if GUICtrlRead($button) = "text2" Then
GUICtrlSetData($button, "text1")
Else
GUICtrlSetData($button, "text2")
EndIf

ExitLoop
Case $GUI_EVENT_MINIMIZE
GUISetState(@SW_HIDE)
EndSwitch
WEnd
EndSwitch
WEnd
Edited by j0kky

Share this post


Link to post
Share on other sites

Did you really think that post was going to cause forum members to come running to assist you with your script?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

hard to notice dumb error but i think i got it finaly

#include <GUIConstantsEx.au3>
Opt("TrayIconDebug", 1)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetOnEvent(-13, "_ripristina_finestra")
Global $My_handle = GUICreate("Window x", 425, 164, 192, 124)
Global $button = GUICtrlCreateButton("text1", 24, 16, 161, 49)
Global $file_ini = @TempDir & "123myfileini.ini"
Local $var_esiste = FileExists($file_ini), $dati_ini
If $var_esiste = 0 Then
    Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID],["Handle", $My_handle]]
    IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)
Else
    $dati_ini = IniReadSection($file_ini, "Sezione1") ;memorizza le chiavi del file ini in un array
    If @error Then
        MsgBox(16, "Errore", "Si e verificato un errore nell'apertura del file .ini")
        FileDelete($file_ini)
        Exit
    EndIf
    Local $var_nome_processo = $dati_ini[1][1], $var_PID = $dati_ini[2][1], $var_handle = HWnd($dati_ini[3][1])
    Local $lista_processi = ProcessList($var_nome_processo)
    If $lista_processi[0][0] > 0 Then
        For $i = 1 To $lista_processi[0][0]
            If $lista_processi[$i][1] = $var_PID Then
                _ripristina_finestra()
                Exit
            EndIf
        Next
    EndIf
    FileDelete($file_ini)
    Dim $dati_ini[3][2] = [["Nome_processo", @ScriptName],["PID_processo", @AutoItPID],["Handle", $My_handle]]
    IniWriteSection($file_ini, "Sezione1", $dati_ini, 0)
EndIf
GUISetState()
While 1
    $a = GUIGetMsg()
    If $a <> 0 And $a <> -11 Then ConsoleWrite($button & ' ' & $a & @CRLF)
    Select
        Case $a = $GUI_EVENT_CLOSE
            Exit
        Case $a = $button
            If GUICtrlRead($button) = "text2" Then
                GUICtrlSetData($button, "text1")
            Else
                GUICtrlSetData($button, "text2")
            EndIf
        Case $a = $GUI_EVENT_MINIMIZE
            WinSetState(@SW_HIDE,'' , $My_handle)
    EndSelect
WEnd
Func _ripristina_finestra()
    Local $state = WinGetState($My_handle)
    If $state = 21 Then
        WinSetState(@SW_SHOW,'', $My_handle)
    Else
        WinSetState(@SW_HIDE,'', $My_handle)
    EndIf
EndFunc   ;==>_ripristina_finestra

basicly it is GUISetState in _ripristina_finestra and not WinSetState, so i revrited some parts but it still works. or other way around on button click.

Gui commands are meant primary for inscript autoit Gui-s (i guess), and win commands for controlling everything else.

Even if you use something try not to mix 2 things together.

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@ JLogan3o13 (I've decided to interpretate seriously your post and not consider it just a stupid flame):

Yes, I thought that my post helps myself to be assisted because of three reasons:

1) Unfortunately if a thread is not among the top tenfifteen, the chances to be responded are very low. Because of it, I wrote "UP" post every three days;

2) That song was more pleasant than monotonous "UP" post;

3) I'm sure listening to that song was nice for bogQ while he was writing code.

Your help is welcome!

@ bogQ: Thank you very much! When I have some free time I'll check your code!

Edited by j0kky

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