Jump to content

Multiple Instances (Unwanted)


bkazdan
 Share

Recommended Posts

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
Link to comment
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
Link to comment
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?

Link to comment
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
Link to comment
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.
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...