Sign in to follow this  
Followers 0
bkazdan

Multiple Instances (Unwanted)

7 posts in this topic

I have created a very basic GUI tool that maps a network drive. It does exactly what I need it to do and works great on my development machine.

Unfortunately on two other machines when it is compiled and launched, it launches with multiple instances.

* All of the machines are running Windows Vista business edition.

* Vista's UAC is turned off on the development machine and is turned on for both of the problem machines.

* Problem machine number 1 launches 178 instances of the application and crashes the computer.

* Problem machine number 2 launches 2 instances of the application. Once one is closed the application runs fine.

I must confess, about a year ago I was scripting much more often and have not been as active over the last few months. I took the following line from a project I worked on over a year ago:

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

I think it might be the problem, but I am not sure why. The full code is listed below. I appreciate any help that I get.

Thanks.

Opt("GUIOnEventMode", 1) 

#include <WinAPI.au3>;
#include <GUIConstants.au3>

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

; Default Values
Global $Drive = "Q:"
Global $Server = "server"
Global $Share = @UserName

; Create Form
$Form_MainWindow = GUICreate("Connect", 165, 250, 100,100)
GUICtrlCreateLabel("Network Drive",50,10) 
GUICtrlCreateLabel("Drive: ",15,40) 
$Combo_Drive = GUICtrlCreateCombo("",50,40,100,20); create combo box
GUICtrlSetData($Combo_Drive, "A:|B:|C:|D:|E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:", $Drive) ;combo box values
GUICtrlCreateLabel("Server: ",15,70)
$Input_Server = GUICtrlCreateInput($Server,50,70,100,20)
GUICtrlCreateLabel("Share: ",15,100)
$Input_Share = GUICtrlCreateInput(@UserName,50,100,100,20)
$Button_MapShare = GUICtrlCreateButton("Connect", 15,130,135,20)
GUICtrlCreateLabel("Status: ",15,160)
$Label_Status = GUICtrlCreateLabel("",15,175,135,75)

GUISetState(@SW_SHOW)   

; Set Buttons & Keys
GUICtrlSetOnEvent($Button_MapShare, "MapShare")
GUISetOnEvent($GUI_EVENT_CLOSE,"OnAutoItExit")
HotKeySet("{Esc}", "OnAutoItExit")


; Keep GUI Open
While 1
    Sleep(1000)
WEnd

; Exit Function
Func OnAutoItExit()
    Exit
EndFunc

; Mapping Function
Func MapShare()
    $Drive = GUICtrlRead($Combo_Drive)
    $Server = GUICtrlRead($Input_Server)
    $Share = GUICtrlRead($Input_Share)
    DriveMapDel($Drive)
    Local $Path = "\\" & $Server & "\" & $Share
    Local $Error_Mapping = DriveMapAdd($Drive,$Path)
    If $Error_Mapping = 1 Then
        GUICtrlSetData($Label_Status,$Path & " has been mapped to " & $Drive)
    EndIf
    If $Error_Mapping = 0 Then
        GUICtrlSetData($Label_Status,$Path & " Has Failed.")
    EndIf
EndFunc

Share this post


Link to post
Share on other sites



@bkazdan...Did u try _Singleton as mentioned in the Helpfile?

:)

Share this post


Link to post
Share on other sites

Open you AutoIt Help File and look for _SingleTon()

with SingleTon() you can check if there are no other occurrences running


[center]I want to change the world ...., but I don't have the source code xD[/center]

Share this post


Link to post
Share on other sites

I have created a very basic GUI tool that maps a network drive. It does exactly what I need it to do and works great on my development machine.

Unfortunately on two other machines when it is compiled and launched, it launches with multiple instances.

* All of the machines are running Windows Vista business edition.

* Vista's UAC is turned off on the development machine and is turned on for both of the problem machines.

* Problem machine number 1 launches 178 instances of the application and crashes the computer.

* Problem machine number 2 launches 2 instances of the application. Once one is closed the application runs fine.

I must confess, about a year ago I was scripting much more often and have not been as active over the last few months. I took the following line from a project I worked on over a year ago:

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

I think it might be the problem, but I am not sure why. The full code is listed below. I appreciate any help that I get.

Thanks.

Opt("GUIOnEventMode", 1) 

#include <WinAPI.au3>;
#include <GUIConstants.au3>

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")
Make sure you have the latest version (3.2.12.1 Production) and look in the help file for "#RequireAdmin" and "AutoIt on Windows Vista".

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I took a step back and thought about the process itself. Mapping drives does not require admin escalation. So I removed the following line:

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

On the machine that was loading 2 copies of the code, it ran fine. I won't have access to the other machine for another 20 minutes or so.

I would like to further discuss _SingleTon() though. What would I place as the text for $sOccurenceName? Is there a way to automatically put the name of the instance there without hardcoding it?

Share this post


Link to post
Share on other sites

I took a step back and thought about the process itself. Mapping drives does not require admin escalation. So I removed the following line:

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

On the machine that was loading 2 copies of the code, it ran fine. I won't have access to the other machine for another 20 minutes or so.

I would like to further discuss _SingleTon() though. What would I place as the text for $sOccurenceName? Is there a way to automatically put the name of the instance there without hardcoding it?

The occurrence name can be anything you want, like: "This_is_my_script_There_are_many_like_it_But_this_one_is_mine". Don't go crazy with variables here, because the instance running _Singleton() has to know what name it's looking for to tell if there's another already running. If the first instance uses a variable name, how will the following instance(s) know what name to look for?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I took a step back and thought about the process itself. Mapping drives does not require admin escalation. So I removed the following line:

If Not IsAdmin() Then ShellExecute (@AutoItEXE, $CmdLineRaw, @WorkingDir, "runas")

On the machine that was loading 2 copies of the code, it ran fine. I won't have access to the other machine for another 20 minutes or so.

I would like to further discuss _SingleTon() though. What would I place as the text for $sOccurenceName? Is there a way to automatically put the name of the instance there without hardcoding it?

Adding to what PsaltyDS said, it might be worth explaining _Singleton a bit more. Windows can keep a special list of strings where there is only one of each string. (Mutually exclusive string called a Mutex) You can add a new string to the list (register) with the call to the kernell32 function CreateMutex which is what _Singleton does. If you register a string which is unusual enough that only your script has it then it is unlikely to already be in the list unless it has been put there by anoth instance of the same program. So if the call to CreateMutex in singleton shows that the string is already in the list then you can assume the script is already ruinning. So the string registered has to be the same for any instance o fthe script so having a variable for the string is of no, or little, benefit.

I don't particuarly like the _Singleton function and I prefer to use _MutexExists.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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