Jump to content

Can I run a program in the background.


 Share

Recommended Posts

Hello I have a simple program to print stuff threw my printer however the script stalls until the printer is done printing.

Is there any way to run a program and instantly continue with the script?  

 

$forcount = ($readct)
For $i = $forcount To 1 Step -1
    ShellExecute(@ScriptDir & "\MOBO.label") ;this is where the program stalls until its done with the print.
 Next

 

Thanks in advance please let me know if you have any questions or ideas.

Link to comment
Share on other sites

  • Moderators

@magace It isn't the ShellExecute that is causing your script to hang, but the For Loop. It is going to pause until it gets through however many iterations there are in $forcount. 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

Then you need to provide your entire script. ShellExecute does not pause the script; you would need to use ShellExecuteWait to achieve this. Something else is causing the delay.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Here is the script and a little info on how it works.

 When I scan a serial number into the s/n box my scanner auto tabs to the next box.  The script detects the box is focused and initiates the Sendit() function.   This function edits the mobo.lable file and then runs it and brings the printer gui back the top for another scan.  When I scan again it goes to the next box where it should que up the next print but it wont start until the other label is finished printing.

 

#include <Array.au3>
#include <File.au3>
#include <Date.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <FileConstants.au3>
#include '_Startup.au3'
Opt("TrayIconDebug", 1)

Local $hDLL = DllOpen("user32.dll")
readconfig()
Example()

Func Example()
    Local $beepc = "1000"
   Local $randbeep = False
   Local $sMsg1 = ""
   Local $hGUI = GUICreate("MY NEW PRINTER", 320, 120, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, $WS_EX_ACCEPTFILES)
    Local $sku = GUICtrlCreateInput($lsku, 30, 5, 280, 20)
    Local $skul = GUICtrlCreateLabel("SKU",5,5,25,20)
    Local $snl = GUICtrlCreateLabel("S/N",5,35,25,20)
     Local $sn = GUICtrlCreateInput("", 30, 35, 250, 20)
     Local $sn3 = GUICtrlCreateInput("", 290, 35, 10, 20)
    Local $cnl = GUICtrlCreateLabel("QTY",250,55,25,20)
    Local $cn = GUICtrlCreateInput("1", 245, 75, 25, 20)
    Local $bc = GUICtrlCreateLabel("BEEP",280,55,30,20)
    Local $bc = GUICtrlCreateInput("500", 275, 75, 35, 20)
    Local $idBtn = GUICtrlCreateButton("Print", 5, 75, 60, 20)
    Local $setup = GUICtrlCreateButton("Setup", 68, 75, 60, 20)
    Local $ucounts = GUICtrlCreateButton("Counts", 130, 75, 60, 20)
    Local $lg1 = GUICtrlCreateLabel("USER",195,55,45,20)
    Local $lg2 = GUICtrlCreateInput("9949", 190, 75, 45, 20)
   Local $snl1 = GUICtrlCreateLabel("S/N LOOKUP",5,100,105,20)
   Local $snl2 = GUICtrlCreateInput("", 80, 95, 150, 20)
   Local $snl3 = GUICtrlCreateButton("LOOKUP", 235, 95, 60, 20)
   Local $idCheckbox = GUICtrlCreateCheckbox("Random Beep", 15,55,100,20)
   Local $startup = GUICtrlCreateCheckbox("AutoStart", 125,55,70,20)
    GUISetState(@SW_SHOW)

GUICtrlSetData($bc,$beepc)
 GUICtrlSetData($lg2,$usr)
 if $astart = "TRUE" Then
    GUICtrlSetState(-1,$startup)
 Else
    GUICtrlSetState(1,$startup)
 EndIf
    While 1
$win = "MY NEW PRINTER"
If WinActive($win) Then
   If _IsPressed("26", $hDLL) Then ;up arrow
WinActivate("MY NEW PRINTER")
ControlClick("MY NEW PRINTER","",3,"LEFT",2,261,12)
      EndIf
   If _IsPressed("28", $hDLL) Then ;down arrow
ControlClick("MY NEW PRINTER","",5,"LEFT",2,261,16)
      EndIf
EndIf
   ToolTip("",0,0)
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
             Case $startup
                If _IsChecked($startup) Then
                  IniWrite(@ScriptDir & "\config.ini", "General", "AUTOSTART","TRUE")
                  _StartupRegistry_Install()
                Else
                  IniWrite(@ScriptDir & "\config.ini", "General", "AUTOSTART","FALSE")
                  _StartupRegistry_Uninstall()
                 EndIf
            Case $idCheckbox
                If _IsChecked($idCheckbox) Then
                  Local $randbeep = True
                Else
                  Local $randbeep = False
                 EndIf
             Case $idBtn
                Global $readsku = (GUICtrlRead($sku))
                IniWrite(@ScriptDir & "\config.ini", "General", "LASTSKU",$readsku)
                Global $readsn  = (GUICtrlRead($sn))
                Global $readct  = (GUICtrlRead($cn))
                Global $usser  = (GUICtrlRead($lg2))
                IniWrite(@ScriptDir & "\config.ini", "General", "USER",$usser)
                If $randbeep = False Then
               IniWrite(@ScriptDir & "\config.ini", "General", "BEEP",GUICtrlRead($bc))
               Global $beepc = IniRead(@ScriptDir & "\config.ini", "General", "BEEP", "Default Value")
               GUICtrlSetData($bc,$beepc)
            Else
                Global $beepc = Random(37,32767)
               GUICtrlSetData($bc,$beepc)
               EndIf
               logdata($usser,$readsku,$readsn)
                Sendit()
             Case $setup
             Case $ucounts
                  Local $sAnswer = InputBox("Question", "What user number?", "9949", "", _
             - 1, -1, 0, 0)
                   Run("\\Teesttterrr-pc\pxe\programs\DYMO\date.exe")
         Case $snl3  ;lookup
             Global $snsearch  = (GUICtrlRead($snl2))
             ToolTip("looking up " & $snsearch,0,0)
             Sleep(500)
            $file = "\\Teesttterrr-pc\pxe\programs\DYMO\printer\logs\log.txt"
            FileOpen($file, 0)
            For $i = 1 to _FileCountLines($file)
                $line = FileReadLine($file, $i)
            ;msgbox(0,'','the line ' & $i & ' is ' & $line)
            if StringInStr($line,$snsearch) Then
               ToolTip("line number " & $i,0,0)
               MsgBox(0,'',FileReadLine($file,$i))
               Sleep(500)
               EndIf
            Next
            FileClose($file)
                Case Else
                $sFocus = ControlGetFocus($hGUI)
                $hFocus = ControlGetHandle($hGUI, "", $sFocus)
                $ctrlFocus = _WinAPI_GetDlgCtrlID($hFocus)
                $sMsg = "Focus:" & @CRLF & _
                        @TAB & "ClassNameNN = " & $sFocus & @CRLF & _
                        @TAB & "Hwnd = " & $hFocus & @CRLF & _
                        @TAB & "ID = " & $ctrlFocus
                        if $ctrlFocus = "7" Then
                        ToolTip("found id5",0,0)
                                        Global $readsku = (GUICtrlRead($sku))
                IniWrite(@ScriptDir & "\config.ini", "General", "LASTSKU",$readsku)
                Global $readsn  = (GUICtrlRead($sn))
                Global $readct  = (GUICtrlRead($cn))
                Global $usser  = (GUICtrlRead($lg2))
                IniWrite(@ScriptDir & "\config.ini", "General", "USER",$usser)
                If $randbeep = False Then
               IniWrite(@ScriptDir & "\config.ini", "General", "BEEP",GUICtrlRead($bc))
               Global $beepc = IniRead(@ScriptDir & "\config.ini", "General", "BEEP", "Default Value")
               GUICtrlSetData($bc,$beepc)
            Else
                Global $beepc = Random(37,32767)
               GUICtrlSetData($bc,$beepc)
               EndIf
                logdata($usser,$readsku,$readsn)
               ;logwhatever
                Sendit()
                        EndIf
                If $sMsg <> $sMsg1 Then
                    ConsoleWrite($sMsg & @CRLF)
                    $sMsg1 = $sMsg
                 EndIf
                 ToolTip("",0,0)
        EndSwitch
    WEnd
DllClose($hDLL)
EndFunc   ;==>Example


Func Sendit()
   $LINE1 = FileReadLine(@ScriptDir & "\MOBO.LABEL",25)
   Local $SKPOS1 = StringInStr($LINE1 , "<String>")+8
   Local $SKPOS2 = StringInStr($LINE1 , "</String>")-9
   Local $SKMID = StringMid($LINE1,$SKPOS1,$SKPOS2)
   Local $SKNEW = ($readsku)
   ToolTip($SKMID)
   _FileWriteToLine(@ScriptDir & "\MOBO.LABEL", 25, "<String>" & StringUpper($SKNEW) & "</String>" , True)
   $LINE1 = FileReadLine(@ScriptDir & "\MOBO.LABEL",49)
   Local $SNPOS1 = StringInStr($LINE1 , "<Text>") + 6
   Local $SNPOS2 = StringInStr($LINE1 , "</Text>") -7
   Local $SNMID = StringMid($LINE1,$SNPOS1,$SNPOS2)
   Local $SNNEW = ($readsn)
   ToolTip($SNMID)
   _FileWriteToLine(@ScriptDir & "\MOBO.LABEL", 49, "<Text>" & $SNNEW & "</Text>" , True)
   WinActivate("MY NEW PRINTER")
   ControlClick("MY NEW PRINTER","",6,"LEFT",2,111,16)
   Send("^a")
   $forcount = ($readct)
   For $i = $forcount To 1 Step -1
       ShellExecute(@ScriptDir & "\MOBO.label")
    Next
    Local $hWnd = WinWait("[CLASS:ConsoleWindowClass]", "", 0)
    WinSetState($hWnd, "", @SW_HIDE)
   Sleep(500)
   Beep($beepc,1000)
EndFunc

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

func logdata($data1,$data2,$data3)
   Local $hFileOpen = FileOpen("\\Teesttterrr-pc\pxe\programs\DYMO\printer\logs\log.txt", $FO_APPEND)
   FileWriteLine($hFileOpen, "DATE:" & _NowDate() & " |  TIME:" & _NowTime() & " |  USER:" & $data1 & "  | SKU:" & $data2 & "  |   S/N:" & $data3  &  @CRLF)
   FileClose($hFileOpen)
EndFunc

func readconfig()
   Global $beepc = IniRead(@ScriptDir & "\config.ini", "General", "BEEP", "Default Value")
   Global $lsku = IniRead(@ScriptDir & "\config.ini", "General", "LASTSKU", "Default Value")
   Global $usr = IniRead(@ScriptDir & "\config.ini", "General", "USER", "Default Value")
   Global $astart = IniRead(@ScriptDir & "\config.ini", "General", "AUTOSTART", "Default Value")
EndFunc

 

Capture.PNG

Edited by magace
Link to comment
Share on other sites

  • Moderators

So, prior to going through your script line by line, have you tried just adding Opt(TrayIconDebug, 1) at the top of your script? Then, when it hangs, you can hover over the icon in the systray and see what line it is waiting for.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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