Sign in to follow this  
Followers 0
gazzer82

Exit Routine

2 posts in this topic

I have been working on my first AutoIt application, it is a background process that watches for changes to a file, if it detects a change it informs the user, parses the data, and outputs the data as keystrokes to a specific application. This seems to be working ok, the bit i am struggling with is how to quit the application.

It has no GUI and should run in the background/system tray at all times, however i may need to manually quit it on occasion. I have tried the right-click exit on the icon in the dock but that doesn't work.

Any help on this would be greatly appreciated, i have posted the script below.

Thanks

Gareth

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=Builds\Barcode Watcher Beta 1.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <array.au3>
#include <file.au3>

;Setup the Variables
Global $sFile = "C:\Barcode_Data\Data1.txt"
Global $aLineArray[1], $aSecond[1], $aThird[1], $aThirdstripped[1], $aSplit[1], $aBarcode, $aQuantity, $ProcessName = "hirepnt.exe", $nFirstlook

;Setup WMI Event for File Watching
$nFirstlook = 1
$file = "C:\Barcode_Data\Data2.txt"
$sComputer = @ComputerName
$oWMIService = ObjGet("winmgmts:" _
         & "{impersonationLevel=impersonate}!\\" & $sComputer & "\root\cimv2")
$colMonitoredEvents = $oWMIService.ExecNotificationQuery _
        ("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " _
         & "TargetInstance ISA 'CIM_DataFile' AND " _
         & "TargetInstance.Name='" & StringReplace($file, '\', '\\') & "'")
;Start watching the files
While 1
    ; Reset timer to zero
    $time = TimerInit()
    ; Monitor file for event
    $oLatestEvent = $colMonitoredEvents.NextEvent
        ; Output if event older then 5 minutes
        If TimerDiff($time) > (3000) Then
        ;   If this is the first loop then ignore the file change
            If $nFirstlook = 1 Then
            $nFirstlook = 0
            ;Otherwise continue with the script
            Else
            ;Ask if we would like to read the barcode data and continue
                If MsgBox(4096+4, "Barcodes Detected", "Would you like to input Barcode Data?", 10) = 6 Then
                    ;Check to see if the process we are targetting is running
                        If ProcessExists($ProcessName) Then
                            ;Read the File into an array
                            _FileReadToArray($sFile, $aLineArray)
                                If @error = 0 Then
                                    ;_ArrayDisplay($aLineArray, "Debug: Read File To Array")
                                Else
                                    MsgBox(16, "Error", "Error reading file: " & $sFile)
                                Exit
                            EndIf
                        ; Set array sizes to match data
                        ReDim $aSecond[$aLineArray[0] + 1] ; Make array size match
                        $aSecond[0] = $aLineArray[0]
                        ReDim $aThird[$aLineArray[0] + 1] ; Make array size match
                        $aThird[0] = $aLineArray[0]
                        ReDim $aThirdstripped[$aLineArray[0] + 1] ; Make array size match
                        $aThirdstripped[0] = $aLineArray[0]


                        For $n = 1 To $aLineArray[0]
                            ;Split each line of the file into an array
                            $aSplit = StringSplit($aLineArray[$n], ",")

                            ;Put parts in arrays
                            If IsArray($aSplit) And $aSplit[0] >= 2 Then
                                $aSecond[$n] = $aSplit[1]
                                    if $aSplit[2] = "  " then
                                        $aThird[$n] = "1 "
                                    else
                                        $aThird[$n] = $aSplit[2]
                                    EndIf
                            EndIf
                        Next

                        ;Remove trailing spaces on numbers
                        For $n4 = 1 to $aThird[0]
                            $aThirdstripped[$n4] = StringStripWS ($aThird[$n4], 2); strip trailing whitespace
                        Next

                        ;Display the arrays for debugging purposes
                        ;_ArrayDisplay($aSecond, "Debug: $aSecond")
                        ;_ArrayDisplay($aThirdstripped, "Debug: $aThird")

                        ;Check to see if Rentalpoint is active, if not make it so
                        If Not WinActive("Check out","") Then WinActivate("Check out","")
                        ;If Not WinActive("Untitled","") Then WinActivate("Untitled","")

                        ;Print the Barcodes out the quaitity of times indicated in the quanity array
                        For $n2 = 1 To $aLineArray[0]
                            $aBarcode = $aSecond[$n2]
                            $aQuantity = $aThirdstripped[$n2]
                            $n3 = 0
                            While $n3 < $aThirdstripped[$n2]
                                $n3 += 1
                                Send ($aBarcode)
                                sleep (100)
                            WEnd
                        Next
                        MsgBox(0,"All Done","Data Input Complete")
                    Else
                        MsgBox(0,"Not Running",$ProcessName & " is not running.")
                    EndIf
            EndIf
        EndIf
    EndIf
WEnd

Share this post


Link to post
Share on other sites



You could set an hotkey not used by any other application to exit. See the example in "HotKeySet". The ESC key terminates the program.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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