Jump to content

Help Creating Independent script


Go to solution Solved by Melba23,

Recommended Posts

Hello folks,

I have been trying to create a gui in auto-it which scans usb devices.

Here,I am handling multiple GUIs at the same time,but the scanner function is not called by any function,it is triggered as soon as any device is connected.

The problem I am facing is that if two GUIs are active at the same time, the scanner function executes twice, If three, then three times... and so on. This is quite undesirable...

Kindly offer a solution.

I am posting a test script of the actual code.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$DBT_DEVICEARRIVAL = "0x00008000"
GUIRegisterMsg($WM_DEVICECHANGE , "_Auto") ;Setting listener for device arrival

#Region ### START Koda GUI section ### Form=
$Test_GUI = GUICreate("Test_GUI", 306, 307, 192, 124)
$Button1 = GUICtrlCreateButton("F1", 16, 16, 81, 81)
$Button2 = GUICtrlCreateButton("F2", 112, 16, 81, 81)
$Button3 = GUICtrlCreateButton("F3", 208, 16, 81, 81)
$Button4 = GUICtrlCreateButton("F4", 16, 112, 81, 81)
$Button5 = GUICtrlCreateButton("F5", 112, 112, 81, 81)
$Button6 = GUICtrlCreateButton("F6", 208, 112, 81, 81)
$Button7 = GUICtrlCreateButton("F7", 16, 208, 81, 81)
$Button8 = GUICtrlCreateButton("F8", 112, 208, 81, 81)
$Button9 = GUICtrlCreateButton("F9", 208, 208, 81, 81)
GUISetState(@SW_SHOW,$Test_GUI)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Select
         Case $nMsg=$GUI_EVENT_CLOSE
            Exit
         Case $nMsg=$Button1 Or $nMsg=$Button2 Or $nMsg=$Button3 Or $nMsg=$Button4 Or $nMsg=$Button5 Or $nMsg=$Button6 Or $nMsg=$Button7 Or $nMsg=$Button8 Or $nMsg=$Button9
            _Rome()
    EndSelect
WEnd

Func _Rome()
   #Region ### START Koda GUI section ### Form=
$Rome = GUICreate("Rome", 261, 182, 325, 182)
$Label1 = GUICtrlCreateLabel("Rome was not Built in a day", 16, 48, 234, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Agree = GUICtrlCreateButton("Ok...I Agree", 40, 104, 177, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW,$Rome)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
         Case $GUI_EVENT_CLOSE
            GUIDelete($Rome)
            Return
         Case $Agree
            GUIDelete($Rome)
            Return       
    EndSwitch
WEnd
EndFunc

Func _Auto($hWndGUI, $MsgID, $WParam, $LParam)
         If $WParam == $DBT_DEVICEARRIVAL Then
         
         ;;Get Removable devices names
         $RemFix = DriveGetDrive( "REMOVABLE" )
         If($RemFix = '') Then
            Return
         Endif
               
         ;;Creation of GUI
         $child = GUICreate("Scanning", 300, 80, @DesktopWidth - 310, @DesktopHeight - 120, BitOR($WS_SYSMENU,$WS_POPUP))
         GUICtrlCreateLabel("Scanning", 10,10)
         $Prog=GUICtrlCreateProgress(10 , 30, 280, 20)
         $StatusInfo=GUICtrlCreateLabel("Status:Ready    ", 10,60)
         $DirInfo=GUICtrlCreateLabel("Drive:", 255,60)
         $DirName=GUICtrlCreateLabel("           ",285,60)
         For $i = 1 to $RemFix[0]
                     If $RemFix[$i] == "c:" Or $RemFix[$i] == "d:" Or $RemFix[$i] == "e:" Or $RemFix[$i] == "f:" Or $RemFix[$i] == "g:" Or $RemFix[$i] == "h:" Or $RemFix[$i] == "i:" Or $RemFix[$i] == "j:" Or $RemFix[$i] == "k:" Or $RemFix[$i] == "l:" Or $RemFix[$i] == "m:" Or $RemFix[$i] == "n:" Or $RemFix[$i] == "o:" Or $RemFix[$i] == "p:" Or $RemFix[$i] == "q:" Or $RemFix[$i] == "r:" Or $RemFix[$i] == "s:" Or $RemFix[$i] == "t:" Or $RemFix[$i] == "u:" Or $RemFix[$i] == "v:" Or $RemFix[$i] == "w:" Or $RemFix[$i] == "x:" Or $RemFix[$i] == "y:" Or $RemFix[$i] == "z:" Then
                           GUISetState(@SW_SHOW)            
                           FileChangeDir ( $RemFix[$i] )
                           GUICtrlSetData($StatusInfo,"Status:Scanning")
                           GUICtrlSetData($DirName,$RemFix[$i])
                           For $val=0 To 100
                           GUICtrlSetData($Prog,$val)
                           Sleep(10)
                           Next
                        Endif
               GUICtrlSetData($StatusInfo,"Status:Ready")
               GUICtrlSetData($DirInfo,"Drive: ")
               Next
             GUIDelete ($child)  
          EndIf
       EndFunc ;==>MyFunc
I can fix this using multiple gui handling, I have been through the entire article. But, since my script contains more than 10 GUIs, I will have to program the entire script again, and that's gonna take a hell lot of time.

So, I need a particular solution for the scanner function, and not for the multiple gui

Umm...Sorry Melba23, you got me wrong.

I want every usb device to be scanned whenever i connect a usb device.

Try clicking on any button, when the gui displays, "Rome was not built in a day", the connect the USB device. The whole scanner program will run twice.

HOW TO AVOID THAT??

Edited by carteblanche619
Added tags
Link to comment
Share on other sites

  • Moderators

carteblanche619,

Welcome to the AutoIt forum. :)

When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

To handle multiple GUIs I recommend you read the wiki about this subject.

BTW: Could you please enclose your code in AutoIt tags (the blue A icon in the editor)? That greatly enhances readability :)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

carteblanche619,

By "if two GUIs are active at the same time, the scanner function executes twice, If three, then three times... and so on" you mean that every attached drive is scanned when a new drive is attached then both the reason and the solution are simple. :)

In your _Auto function, you are filling the $RemFix array with all the removeable drives each time it is run - and then looping through that array, which is why you scan each drive. What you need to do is save the drive list from the last time a drive was attached and then go through the new list when the function is next run to find the letter of the new drive - that way you can scan only the new drive. ;)

All clear?

M23

P.S. You might like to look at Switch in the Help file - then you can avoid that ugly $nMsg = $Button1 Or $nMsg = $Button2 Or $nMsg = $Button3 Or.... line. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hello folks,

I am working on a script containing 2 GUI and a scanner.

I am posting a test script of the original script here. In this script, when you click on any button, a child gui opens with message "Rome was not built in a day".

The problem in this script is that when I only open the parent GUI and connect a USB device,scanner triggeres only once. But when I open the child GUI as well, scanner triggeres twice, even though scanner was not called in any of the functions, but triggers only in case of device arrival event.

I want scanner to trigger only once.

Please Help...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$DBT_DEVICEARRIVAL = "0x00008000"
GUIRegisterMsg($WM_DEVICECHANGE , "_Auto") ;Setting listener for device arrival

#Region ### START Koda GUI section ### Form=
$Test_GUI = GUICreate("Test_GUI", 306, 307, 192, 124)
$Button1 = GUICtrlCreateButton("F1", 16, 16, 81, 81)
$Button2 = GUICtrlCreateButton("F2", 112, 16, 81, 81)
$Button3 = GUICtrlCreateButton("F3", 208, 16, 81, 81)
$Button4 = GUICtrlCreateButton("F4", 16, 112, 81, 81)
$Button5 = GUICtrlCreateButton("F5", 112, 112, 81, 81)
$Button6 = GUICtrlCreateButton("F6", 208, 112, 81, 81)
$Button7 = GUICtrlCreateButton("F7", 16, 208, 81, 81)
$Button8 = GUICtrlCreateButton("F8", 112, 208, 81, 81)
$Button9 = GUICtrlCreateButton("F9", 208, 208, 81, 81)
GUISetState(@SW_SHOW,$Test_GUI)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Select
         Case $nMsg=$GUI_EVENT_CLOSE
            Exit
         Case $nMsg=$Button1 Or $nMsg=$Button2 Or $nMsg=$Button3 Or $nMsg=$Button4 Or $nMsg=$Button5 Or $nMsg=$Button6 Or $nMsg=$Button7 Or $nMsg=$Button8 Or $nMsg=$Button9
            _Rome()
    EndSelect
WEnd

Func _Rome()
   #Region ### START Koda GUI section ### Form=
$Rome = GUICreate("Rome", 261, 182, 325, 182)
$Label1 = GUICtrlCreateLabel("Rome was not Built in a day", 16, 48, 234, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Agree = GUICtrlCreateButton("Ok...I Agree", 40, 104, 177, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW,$Rome)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
         Case $GUI_EVENT_CLOSE
            GUIDelete($Rome)
            Return
         Case $Agree
            GUIDelete($Rome)
            Return       
    EndSwitch
WEnd
EndFunc

Func _Auto($hWndGUI, $MsgID, $WParam, $LParam)
         If $WParam == $DBT_DEVICEARRIVAL Then
         
         ;;Get Removable devices names
         $RemFix = DriveGetDrive( "REMOVABLE" )
         If($RemFix = '') Then
            Return
         Endif
               
         ;;Creation of GUI
         $child = GUICreate("Scanning", 300, 80, @DesktopWidth - 310, @DesktopHeight - 120, BitOR($WS_SYSMENU,$WS_POPUP))
         GUICtrlCreateLabel("Scanning", 10,10)
         $Prog=GUICtrlCreateProgress(10 , 30, 280, 20)
         $StatusInfo=GUICtrlCreateLabel("Status:Ready    ", 10,60)
         $DirInfo=GUICtrlCreateLabel("Drive:", 255,60)
         $DirName=GUICtrlCreateLabel("           ",285,60)
         For $i = 1 to $RemFix[0]
                     If $RemFix[$i] == "c:" Or $RemFix[$i] == "d:" Or $RemFix[$i] == "e:" Or $RemFix[$i] == "f:" Or $RemFix[$i] == "g:" Or $RemFix[$i] == "h:" Or $RemFix[$i] == "i:" Or $RemFix[$i] == "j:" Or $RemFix[$i] == "k:" Or $RemFix[$i] == "l:" Or $RemFix[$i] == "m:" Or $RemFix[$i] == "n:" Or $RemFix[$i] == "o:" Or $RemFix[$i] == "p:" Or $RemFix[$i] == "q:" Or $RemFix[$i] == "r:" Or $RemFix[$i] == "s:" Or $RemFix[$i] == "t:" Or $RemFix[$i] == "u:" Or $RemFix[$i] == "v:" Or $RemFix[$i] == "w:" Or $RemFix[$i] == "x:" Or $RemFix[$i] == "y:" Or $RemFix[$i] == "z:" Then
                           GUISetState(@SW_SHOW)            
                           FileChangeDir ( $RemFix[$i] )
                           GUICtrlSetData($StatusInfo,"Status:Scanning")
                           GUICtrlSetData($DirName,$RemFix[$i])
                           For $val=0 To 100
                           GUICtrlSetData($Prog,$val)
                           Sleep(10)
                           Next
                        Endif
               GUICtrlSetData($StatusInfo,"Status:Ready")
               GUICtrlSetData($DirInfo,"Drive: ")
               Next
             GUIDelete ($child)  
          EndIf
       EndFunc ;==>MyFunc
Link to comment
Share on other sites

  • Moderators

carteblanche619,

Threads merged - how about sticking to one at a time in future? :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators
  • Solution

carteblanche619,

The reason for the double scan is that the DEVICEARRIVAL message is being intercepted by both of the GUIs - so you run the function twice. Just check the GUI receiving the message and only action when one does. :)

You are also creating GUI and waiting for user input inside a mesasge handler - this is not a good idea. When the Help file says:

 

"Warning: blocking of running user functions which executes window messages with commands such as "MsgBox()" can lead to unexpected behavior, the return to the system should be as fast as possible !!!"

it means it! :o

Look at this version of your script - it shows one of the ways to run a long or blocking function from within a handler using a dummy control. And in order to speed up the whole thing I have shown how to share an idle loop between the main GUI and the Rome child - if you do not do this then the dummy will not run th scan function until after you close the child and return to the main loop which is probably not what you want. Read the Managing Multiple GUIs tutorial in the Wiki to learm why this happens. :)

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $RemFix, $Rome = 9999, $Agree = 9999, $fScanning = False

$Test_GUI = GUICreate("Test_GUI", 306, 307, 192, 124)
$Button1 = GUICtrlCreateButton("F1", 16, 16, 81, 81)
$Button2 = GUICtrlCreateButton("F2", 112, 16, 81, 81)
$Button3 = GUICtrlCreateButton("F3", 208, 16, 81, 81)
$Button4 = GUICtrlCreateButton("F4", 16, 112, 81, 81)
$Button5 = GUICtrlCreateButton("F5", 112, 112, 81, 81)
$Button6 = GUICtrlCreateButton("F6", 208, 112, 81, 81)
$Button7 = GUICtrlCreateButton("F7", 16, 208, 81, 81)
$Button8 = GUICtrlCreateButton("F8", 112, 208, 81, 81)
$Button9 = GUICtrlCreateButton("F9", 208, 208, 81, 81)

$cScan = GUICtrlCreateDummy()

GUISetState(@SW_SHOW, $Test_GUI)

$DBT_DEVICEARRIVAL = "0x00008000"
GUIRegisterMsg($WM_DEVICECHANGE, "_Auto") ;Setting listener for device arrival

While 1
    $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
        Case $Test_GUI
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $Button1, $Button2, $Button3, $Button4, $Button5, $Button6, $Button7, $Button8, $Button9
                    _Rome()
                Case $cScan
                    _Scan()
                    $fScanning = False
            EndSwitch
        Case $Rome
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE, $Agree
                    GUIDelete($Rome)
                    $Rome = 9999
                    $Agree = 9999
            EndSwitch
    EndSwitch
WEnd

Func _Rome()
    $Rome = GUICreate("Rome", 261, 182, 325, 182)
    $Label1 = GUICtrlCreateLabel("Rome was not Built in a day", 16, 48, 234, 28)
    GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
    $Agree = GUICtrlCreateButton("Ok...I Agree", 40, 104, 177, 41)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW, $Rome)
    Return
EndFunc   ;==>_Rome

Func _Scan()

    ;;Creation of GUI
    $child = GUICreate("Scanning", 300, 80, @DesktopWidth - 310, @DesktopHeight - 120, BitOR($WS_SYSMENU, $WS_POPUP))
    GUICtrlCreateLabel("Scanning", 10, 10)
    $Prog = GUICtrlCreateProgress(10, 30, 280, 20)
    $StatusInfo = GUICtrlCreateLabel("Status:Ready    ", 10, 60)
    $DirInfo = GUICtrlCreateLabel("Drive:", 255, 60)
    $DirName = GUICtrlCreateLabel("           ", 285, 60)
    For $i = 1 To $RemFix[0]
        If $RemFix[$i] == "c:" Or $RemFix[$i] == "d:" Or $RemFix[$i] == "e:" Or $RemFix[$i] == "f:" Or $RemFix[$i] == "g:" Or $RemFix[$i] == "h:" Or $RemFix[$i] == "i:" Or $RemFix[$i] == "j:" Or $RemFix[$i] == "k:" Or $RemFix[$i] == "l:" Or $RemFix[$i] == "m:" Or $RemFix[$i] == "n:" Or $RemFix[$i] == "o:" Or $RemFix[$i] == "p:" Or $RemFix[$i] == "q:" Or $RemFix[$i] == "r:" Or $RemFix[$i] == "s:" Or $RemFix[$i] == "t:" Or $RemFix[$i] == "u:" Or $RemFix[$i] == "v:" Or $RemFix[$i] == "w:" Or $RemFix[$i] == "x:" Or $RemFix[$i] == "y:" Or $RemFix[$i] == "z:" Then
            GUISetState(@SW_SHOW)
            FileChangeDir($RemFix[$i])
            GUICtrlSetData($StatusInfo, "Status:Scanning")
            GUICtrlSetData($DirName, $RemFix[$i])
            For $val = 0 To 100
                GUICtrlSetData($Prog, $val)
                Sleep(10)
            Next
        EndIf
        GUICtrlSetData($StatusInfo, "Status:Ready")
        GUICtrlSetData($DirInfo, "Drive: ")
    Next
    GUIDelete($child)

EndFunc   ;==>_Scan

Func _Auto($hWndGUI, $MsgID, $WParam, $LParam)

    If $hWndGUI = $Test_GUI And $WParam = $DBT_DEVICEARRIVAL Then

        ;;Get Removable devices names
        $RemFix = DriveGetDrive("REMOVABLE")
        If $RemFix = '' Then
            Return
        Else
            GUICtrlSendToDummy($cScan)
        EndIf

    EndIf
EndFunc   ;==>_Auto
Please ask (in this thread!) if you have any questions. ;)

M23

Edited by Melba23
Helps if I post the script!

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...