Jump to content
Sign in to follow this  
supersonic

Can't close program / high CPU usage

Recommended Posts

supersonic

Hello everybody,

I wrote a little script to print directly to LPT1:. Theoretically it will work, but two problems prevent me from finishing this script:

1. I can't stop the program.

2. High CPU usage (maybe due GUICtrlSetOnEvent() or badly programmed While/WEnd).

Can somebody help me out...

#include <GUIConstants.au3>
#include <GUIListView.au3>


; Preserve order of variables!
Global $program     = "DPRINT"
Global $lCounter    = 0
Global $pCounter    = 0
Global $pScope      = RegRead("HKCU\Software\" & $program, "pScope")
Global $fScope      = RegRead("HKCU\Software\" & $program, "fScope")
Global $port        = RegRead("HKCU\Software\" & $program, "Port")
Global $timer       = RegRead("HKCU\Software\" & $program, "Timer")
; <...>
Global $mainGUI     = GUICreate($program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">", 500, 376, -1, -1)
; Global $mainP     = GUICtrlCreatePic(@ScriptDir & "\" & $program & ".bmp", 1, 1, 498, 50)
Global $mainLV      = GUICtrlCreateListView("Date/Time|Path/File", 1, 53, 498, 148, $LVS_SORTDESCENDING, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
Global $mainG1      = GUICtrlCreateGroup("pScope",                  001, 200, 498, 158)
Global $mainG2      = GUICtrlCreateGroup("fScope | Port | Timer",   001, 243, 498, 115)
Global $mainG2      = GUICtrlCreateGroup("",                        001, 286, 498, 072)
Global $mainI       = GUICtrlCreateInput($pScope, 13, 218, 450, 21)
Global $mainC1      = GUICtrlCreateCombo($fScope,   013, 261, 156, 021)
Global $mainC2      = GUICtrlCreateCombo($port,     172, 261, 156, 021)
Global $mainC3      = GUICtrlCreateCombo($timer,    331, 261, 156, 021)
Global $mainB1      = GUICtrlCreateButton(">",      466, 218, 021, 021, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB2      = GUICtrlCreateButton("START",  013, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB3      = GUICtrlCreateButton("STOP",   172, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB4      = GUICtrlCreateButton("CLOSE",  331, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainL       = GUICtrlCreateLabel("Freeware.", 1, 360, 498, 15, $SS_CENTER)


AutoItSetOption("GUIOnEventMode", 1) ; Opt
; AutoItSetOption("TrayIconHide", 1) ; Opt


HotKeySet("^{F1}", "start") ; STRG + F1
HotKeySet("^{F2}", "dummy") ; STRG + F2
HotKeySet("^{F3}", "close") ; STRG + F3


GUICtrlSetBkColor($mainL, 0x484848)
GUICtrlSetColor($mainL, 0xFFFFFF)
GUICtrlSetFont($mainL, 8.5, 400)
GUICtrlSetOnEvent($mainB1, "path")
GUICtrlSetOnEvent($mainB2, "start")
GUICtrlSetOnEvent($mainB3, "stop")
GUICtrlSetOnEvent($mainB4, "close")
GUICtrlSetState($mainI, $GUI_DISABLE)
GUICtrlSetState($mainB2, $GUI_FOCUS)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState(@SW_SHOW, $mainGUI)


$fArray = StringSplit("*.csv;*.dat;*.doc;*.txt", ";", 0)
For $i = 1 to $fArray[0] Step 1
    If Not StringInStr($fArray[$i], $fScope, 0) Then
        GUICtrlSetData($mainC1, $fArray[$i], "")
    EndIf
Next
; <...>
$pArray = StringSplit("COM1:;COM2:;COM3:;COM4:;LPT1:;LPT2:;LPT3:;LPT4:", ";", 0)
For $i = 1 to $pArray[0] Step 1
    If Not StringInStr($pArray[$i], $port, 0) Then
        GUICtrlSetData($mainC2, $pArray[$i], "")
    EndIf
Next
; <...>
For $i = 1 to 30 Step 1
    GUICtrlSetData($mainC3, $i, "")
Next

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Func path()
    $pDummy = FileSelectFolder("", "", 4, $pScope)
    If  $pDummy <> ""                       And _
        Not StringInStr($pDummy, ".",   0)  And _
        Not StringInStr($pDummy, "\\",  0)  Then
                $pScope = $pDummy
                GUICtrlSetData($mainI, $pScope)
    EndIf
EndFunc


Func start()
    GUICtrlSetState($mainC1, $GUI_DISABLE)
    GUICtrlSetState($mainC2, $GUI_DISABLE)
    GUICtrlSetState($mainC3, $GUI_DISABLE)
    GUICtrlSetState($mainB1, $GUI_DISABLE)
    GUICtrlSetState($mainB2, $GUI_DISABLE)
    GUICtrlSetState($mainB3, $GUI_FOCUS)
    ; <...>
    If  StringIsASCII(GUICtrlRead($mainI))  = "" Or _
        StringIsASCII(GUICtrlRead($mainC1)) = "" Or _
        StringIsASCII(GUICtrlRead($mainC2)) = "" Or _
        StringIsInt(GUICtrlRead($mainC3))   = "" Then
            ; <...>
    Else
        For $i = 0 to 1 Step 1
            For $j = $timer to 1 Step -1
                GUICtrlSetData($mainC3, $j, "")
                Sleep(1000)
            Next
            $sDummy = FileFindFirstFile($pScope & "\" & $fScope)
            If $sDummy = -1 Then
                $lCounter = $lCounter + 1
                WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & "No file(s) found.", $mainLV)
                lvAutosize()
            EndIf
            While 1
                $fDummy = FileFindNextFile($sDummy)
                If @error Then
                    ExitLoop
                Else
                    $pCounter = $pCounter + 1
                    WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                    GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & $pScope & "\" & $fDummy, $mainLV)
                    lvAutosize()
                EndIf
            WEnd
            FileClose($sDummy)
            $i = 0
        Next
    EndIf
    ; <...>
    GUICtrlSetState($mainC1, $GUI_ENABLE)
    GUICtrlSetState($mainC2, $GUI_ENABLE)
    GUICtrlSetState($mainC3, $GUI_ENABLE)
    GUICtrlSetState($mainB1, $GUI_ENABLE)
    GUICtrlSetState($mainB2, $GUI_ENABLE)
    GUICtrlSetState($mainB2, $GUI_FOCUS)
EndFunc


Func stop()
EndFunc


Func close()
    If StringIsASCII(GUICtrlRead($mainI)) Then
        RegWrite("HKCU\Software\" & $program, "pScope", "REG_SZ", GUICtrlRead($mainI))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC1)) Then
        RegWrite("HKCU\Software\" & $program, "fScope", "REG_SZ", GUICtrlRead($mainC1))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC2)) Then
        RegWrite("HKCU\Software\" & $program, "Port",   "REG_SZ", GUICtrlRead($mainC2))
    EndIf
    If StringIsInt(GUICtrlRead($mainC3)) Then
        RegWrite("HKCU\Software\" & $program, "Timer",  "REG_SZ", GUICtrlRead($mainC3))
    EndIf
    ; <...>
    Exit
EndFunc


Func dummy()
EndFunc


Func lvAutosize()
    For $i = 0 to 1
        _GUICtrlListView_SetColumnWidth($mainLV, $i, $LVSCW_AUTOSIZE)
    Next
EndFunc

Greets,

-supersonic.

Share this post


Link to post
Share on other sites
Andreik

Use sleep in some loops.

For example:

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
    Sleep(25)
WEnd
Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites
ChrisL

Use sleep in some loops.

For example:

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
    Sleep(25)
WEnd
You don't use a sleep in a GuiGetMsg() loop because it automatically sleeps the CPU

Share this post


Link to post
Share on other sites
supersonic

It's strange...

After START button is pressed I can't press CLOSE button...

But the hotkeys are working...

Any ideas?

Edited by supersonic

Share this post


Link to post
Share on other sites
Andreik

You don't use a sleep in a GuiGetMsg() loop because it automatically sleeps the CPU

I didn't knew this. How much time?


When the words fail... music speaks

Share this post


Link to post
Share on other sites
ChrisL

You don't need GuiGetMsg() its an onEvent Gui

It works fine for me like this (closing that is all I tried) no high CPU

CODE
#include <GUIConstants.au3>

#include <GUIListView.au3>

;#include <ButtonConstants.au3>

;#include <WindowsConstants.au3>

;#include <StaticConstants.au3>

; Preserve order of variables!

Global $program = "DPRINT"

Global $lCounter = 0

Global $pCounter = 0

Global $pScope = RegRead("HKCU\Software\" & $program, "pScope")

Global $fScope = RegRead("HKCU\Software\" & $program, "fScope")

Global $port = RegRead("HKCU\Software\" & $program, "Port")

Global $timer = RegRead("HKCU\Software\" & $program, "Timer")

; <...>

Global $mainGUI = GUICreate($program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">", 500, 376, -1, -1)

; Global $mainP = GUICtrlCreatePic(@ScriptDir & "\" & $program & ".bmp", 1, 1, 498, 50)

Global $mainLV = GUICtrlCreateListView("Date/Time|Path/File", 1, 53, 498, 148, $LVS_SORTDESCENDING, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))

Global $mainG1 = GUICtrlCreateGroup("pScope", 001, 200, 498, 158)

Global $mainG2 = GUICtrlCreateGroup("fScope | Port | Timer", 001, 243, 498, 115)

Global $mainG2 = GUICtrlCreateGroup("", 001, 286, 498, 072)

Global $mainI = GUICtrlCreateInput($pScope, 13, 218, 450, 21)

Global $mainC1 = GUICtrlCreateCombo($fScope, 013, 261, 156, 021)

Global $mainC2 = GUICtrlCreateCombo($port, 172, 261, 156, 021)

Global $mainC3 = GUICtrlCreateCombo($timer, 331, 261, 156, 021)

Global $mainB1 = GUICtrlCreateButton(">", 466, 218, 021, 021, $BS_CENTER, $WS_EX_CLIENTEDGE)

Global $mainB2 = GUICtrlCreateButton("START", 013, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)

Global $mainB3 = GUICtrlCreateButton("STOP", 172, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)

Global $mainB4 = GUICtrlCreateButton("CLOSE", 331, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)

Global $mainL = GUICtrlCreateLabel("Freeware.", 1, 360, 498, 15, $SS_CENTER)

AutoItSetOption("GUIOnEventMode", 1); Opt

; AutoItSetOption("TrayIconHide", 1); Opt

HotKeySet("^{F1}", "start"); STRG + F1

HotKeySet("^{F2}", "dummy"); STRG + F2

HotKeySet("^{F3}", "close"); STRG + F3

GUICtrlSetBkColor($mainL, 0x484848)

GUICtrlSetColor($mainL, 0xFFFFFF)

GUICtrlSetFont($mainL, 8.5, 400)

GUICtrlSetOnEvent($mainB1, "path")

GUICtrlSetOnEvent($mainB2, "start")

GUICtrlSetOnEvent($mainB3, "stop")

GUICtrlSetOnEvent($mainB4, "close")

GUICtrlSetState($mainI, $GUI_DISABLE)

GUICtrlSetState($mainB2, $GUI_FOCUS)

GUISetOnEvent($GUI_EVENT_CLOSE, "close")

GUISetState(@SW_SHOW, $mainGUI)

$fArray = StringSplit("*.csv;*.dat;*.doc;*.txt", ";", 0)

For $i = 1 to $fArray[0] Step 1

If Not StringInStr($fArray[$i], $fScope, 0) Then

GUICtrlSetData($mainC1, $fArray[$i], "")

EndIf

Next

; <...>

$pArray = StringSplit("COM1:;COM2:;COM3:;COM4:;LPT1:;LPT2:;LPT3:;LPT4:", ";", 0)

For $i = 1 to $pArray[0] Step 1

If Not StringInStr($pArray[$i], $port, 0) Then

GUICtrlSetData($mainC2, $pArray[$i], "")

EndIf

Next

; <...>

For $i = 1 to 30 Step 1

GUICtrlSetData($mainC3, $i, "")

Next

While 1

Sleep(1000)

WEnd

Func path()

$pDummy = FileSelectFolder("", "", 4, $pScope)

If $pDummy <> "" And _

Not StringInStr($pDummy, ".", 0) And _

Not StringInStr($pDummy, "\\", 0) Then

$pScope = $pDummy

GUICtrlSetData($mainI, $pScope)

EndIf

EndFunc

Func start()

GUICtrlSetState($mainC1, $GUI_DISABLE)

GUICtrlSetState($mainC2, $GUI_DISABLE)

GUICtrlSetState($mainC3, $GUI_DISABLE)

GUICtrlSetState($mainB1, $GUI_DISABLE)

GUICtrlSetState($mainB2, $GUI_DISABLE)

GUICtrlSetState($mainB3, $GUI_FOCUS)

; <...>

If StringIsASCII(GUICtrlRead($mainI)) = "" Or _

StringIsASCII(GUICtrlRead($mainC1)) = "" Or _

StringIsASCII(GUICtrlRead($mainC2)) = "" Or _

StringIsInt(GUICtrlRead($mainC3)) = "" Then

; <...>

Else

For $i = 0 to 1 Step 1

For $j = $timer to 1 Step -1

GUICtrlSetData($mainC3, $j, "")

Sleep(1000)

Next

$sDummy = FileFindFirstFile($pScope & "\" & $fScope)

If $sDummy = -1 Then

$lCounter = $lCounter + 1

WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")

GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & "No file(s) found.", $mainLV)

lvAutosize()

EndIf

While 1

$fDummy = FileFindNextFile($sDummy)

If @error Then

ExitLoop

Else

$pCounter = $pCounter + 1

WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")

GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & $pScope & "\" & $fDummy, $mainLV)

lvAutosize()

EndIf

WEnd

FileClose($sDummy)

$i = 0

Next

EndIf

; <...>

GUICtrlSetState($mainC1, $GUI_ENABLE)

GUICtrlSetState($mainC2, $GUI_ENABLE)

GUICtrlSetState($mainC3, $GUI_ENABLE)

GUICtrlSetState($mainB1, $GUI_ENABLE)

GUICtrlSetState($mainB2, $GUI_ENABLE)

GUICtrlSetState($mainB2, $GUI_FOCUS)

EndFunc

Func stop()

EndFunc

Func close()

If StringIsASCII(GUICtrlRead($mainI)) Then

RegWrite("HKCU\Software\" & $program, "pScope", "REG_SZ", GUICtrlRead($mainI))

EndIf

If StringIsASCII(GUICtrlRead($mainC1)) Then

RegWrite("HKCU\Software\" & $program, "fScope", "REG_SZ", GUICtrlRead($mainC1))

EndIf

If StringIsASCII(GUICtrlRead($mainC2)) Then

RegWrite("HKCU\Software\" & $program, "Port", "REG_SZ", GUICtrlRead($mainC2))

EndIf

If StringIsInt(GUICtrlRead($mainC3)) Then

RegWrite("HKCU\Software\" & $program, "Timer", "REG_SZ", GUICtrlRead($mainC3))

EndIf

; <...>

Exit

EndFunc

Func dummy()

EndFunc

Func lvAutosize()

For $i = 0 to 1

_GUICtrlListView_SetColumnWidth($mainLV, $i, $LVSCW_AUTOSIZE)

Next

EndFunc

Edited by ChrisL

Share this post


Link to post
Share on other sites
ChrisL

I see what you mean when it's running..

This allows you to use the stop button

Edit: I changed it a bit I like this better!

Edit2: the second exitloop is now Exitloop(1) otherwise it leaves file handles open

#include <GUIConstants.au3>
#include <GUIListView.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3> 


; Preserve order of variables!
Global $program  = "DPRINT"
Global $lCounter    = 0
Global $pCounter    = 0
Global $pScope  = RegRead("HKCU\Software\" & $program, "pScope")
Global $fScope  = RegRead("HKCU\Software\" & $program, "fScope")
Global $port        = RegRead("HKCU\Software\" & $program, "Port")
Global $timer     = RegRead("HKCU\Software\" & $program, "Timer")
; <...>
Global $mainGUI  = GUICreate($program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">", 500, 376, -1, -1)
; Global $mainP  = GUICtrlCreatePic(@ScriptDir & "\" & $program & ".bmp", 1, 1, 498, 50)
Global $mainLV  = GUICtrlCreateListView("Date/Time|Path/File", 1, 53, 498, 148, $LVS_SORTDESCENDING, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
Global $mainG1  = GUICtrlCreateGroup("pScope",                  001, 200, 498, 158)
Global $mainG2  = GUICtrlCreateGroup("fScope | Port | Timer", 001, 243, 498, 115)
Global $mainG2  = GUICtrlCreateGroup("",            001, 286, 498, 072)
Global $mainI     = GUICtrlCreateInput($pScope, 13, 218, 450, 21)
Global $mainC1  = GUICtrlCreateCombo($fScope, 013, 261, 156, 021)
Global $mainC2  = GUICtrlCreateCombo($port,   172, 261, 156, 021)
Global $mainC3  = GUICtrlCreateCombo($timer,  331, 261, 156, 021)
Global $mainB1  = GUICtrlCreateButton(">",      466, 218, 021, 021, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB2  = GUICtrlCreateButton("START",  013, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB3  = GUICtrlCreateButton("STOP", 172, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB4  = GUICtrlCreateButton("CLOSE",  331, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainL     = GUICtrlCreateLabel("Freeware.", 1, 360, 498, 15, $SS_CENTER)
Global $Start

AutoItSetOption("GUIOnEventMode", 1); Opt
; AutoItSetOption("TrayIconHide", 1); Opt


HotKeySet("^{F1}", "start"); STRG + F1
HotKeySet("^{F2}", "dummy"); STRG + F2
HotKeySet("^{F3}", "close"); STRG + F3


GUICtrlSetBkColor($mainL, 0x484848)
GUICtrlSetColor($mainL, 0xFFFFFF)
GUICtrlSetFont($mainL, 8.5, 400)
GUICtrlSetOnEvent($mainB1, "path")
GUICtrlSetOnEvent($mainB2, "start")
GUICtrlSetOnEvent($mainB3, "stop")
GUICtrlSetOnEvent($mainB4, "close")
GUICtrlSetState($mainI, $GUI_DISABLE)
GUICtrlSetState($mainB2, $GUI_FOCUS)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState(@SW_SHOW, $mainGUI)


$fArray = StringSplit("*.csv;*.dat;*.doc;*.txt", ";", 0)
For $i = 1 to $fArray[0] Step 1
    If Not StringInStr($fArray[$i], $fScope, 0) Then
        GUICtrlSetData($mainC1, $fArray[$i], "")
    EndIf
Next
; <...>
$pArray = StringSplit("COM1:;COM2:;COM3:;COM4:;LPT1:;LPT2:;LPT3:;LPT4:", ";", 0)
For $i = 1 to $pArray[0] Step 1
    If Not StringInStr($pArray[$i], $port, 0) Then
        GUICtrlSetData($mainC2, $pArray[$i], "")
    EndIf
Next
; <...>
For $i = 1 to 30 Step 1
    GUICtrlSetData($mainC3, $i, "")
Next

While 1
    Sleep(1000)
    If $Start = 1 then _process()
WEnd

Func _Process()
        GUICtrlSetState($mainC1, $GUI_DISABLE)
        GUICtrlSetState($mainC2, $GUI_DISABLE)
        GUICtrlSetState($mainC3, $GUI_DISABLE)
        GUICtrlSetState($mainB1, $GUI_DISABLE)
        GUICtrlSetState($mainB2, $GUI_DISABLE)
        GUICtrlSetState($mainB3, $GUI_FOCUS)
; <...>
        If  StringIsASCII(GUICtrlRead($mainI))  = "" Or _
            StringIsASCII(GUICtrlRead($mainC1)) = "" Or _
            StringIsASCII(GUICtrlRead($mainC2)) = "" Or _
            StringIsInt(GUICtrlRead($mainC3))   = "" Then
    ; <...>
        Else
            For $i = 0 to 1 Step 1
                For $j = $timer to 1 Step -1
                    If $Start = 0 then Exitloop(2)
                    GUICtrlSetData($mainC3, $j, "")
                    Sleep(1000)
                Next
                $sDummy = FileFindFirstFile($pScope & "\" & $fScope)
                If $sDummy = -1 Then
                    $lCounter = $lCounter + 1
                    WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                    GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & "No file(s) found.", $mainLV)
                    lvAutosize()
                EndIf
                While 1
                    If $Start = 0 then Exitloop(1)
                    $fDummy = FileFindNextFile($sDummy)
                    If @error Then
                        ExitLoop
                    Else
                        $pCounter = $pCounter + 1
                        WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                        GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & $pScope & "\" & $fDummy, $mainLV)
                        lvAutosize()
                    EndIf
                WEnd
                FileClose($sDummy)
                $i = 0
            Next
        EndIf
; <...>
        GUICtrlSetState($mainC1, $GUI_ENABLE)
        GUICtrlSetState($mainC2, $GUI_ENABLE)
        GUICtrlSetState($mainC3, $GUI_ENABLE)
        GUICtrlSetState($mainB1, $GUI_ENABLE)
        GUICtrlSetState($mainB2, $GUI_ENABLE)
        GUICtrlSetState($mainB2, $GUI_FOCUS)
EndFunc
    
Func path()
    $pDummy = FileSelectFolder("", "", 4, $pScope)
    If  $pDummy <> ""     And _
        Not StringInStr($pDummy, ".",   0) And _
        Not StringInStr($pDummy, "\\",  0)  Then
                $pScope = $pDummy
                GUICtrlSetData($mainI, $pScope)
    EndIf
EndFunc


Func start()
    $Start = 1
EndFunc


Func stop()
    $Start = 0
EndFunc


Func close()
    If StringIsASCII(GUICtrlRead($mainI)) Then
        RegWrite("HKCU\Software\" & $program, "pScope", "REG_SZ", GUICtrlRead($mainI))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC1)) Then
        RegWrite("HKCU\Software\" & $program, "fScope", "REG_SZ", GUICtrlRead($mainC1))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC2)) Then
        RegWrite("HKCU\Software\" & $program, "Port",   "REG_SZ", GUICtrlRead($mainC2))
    EndIf
    If StringIsInt(GUICtrlRead($mainC3)) Then
        RegWrite("HKCU\Software\" & $program, "Timer",  "REG_SZ", GUICtrlRead($mainC3))
    EndIf
 ; <...>
    Exit
EndFunc


Func dummy()
EndFunc


Func lvAutosize()
    For $i = 0 to 1
        _GUICtrlListView_SetColumnWidth($mainLV, $i, $LVSCW_AUTOSIZE)
    Next
EndFunc
Edited by ChrisL

Share this post


Link to post
Share on other sites
ChrisL
supersonic

@ChrisL: Thanks very much for your help!

I see what you mean when it's running..

This allows you to use the stop button

Edit: I changed it a bit I like this better!

Edit2: the second exitloop is now Exitloop(1) otherwise it leaves file handles open

#include <GUIConstants.au3>
#include <GUIListView.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3> 


; Preserve order of variables!
Global $program  = "DPRINT"
Global $lCounter    = 0
Global $pCounter    = 0
Global $pScope  = RegRead("HKCU\Software\" & $program, "pScope")
Global $fScope  = RegRead("HKCU\Software\" & $program, "fScope")
Global $port        = RegRead("HKCU\Software\" & $program, "Port")
Global $timer     = RegRead("HKCU\Software\" & $program, "Timer")
; <...>
Global $mainGUI  = GUICreate($program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">", 500, 376, -1, -1)
; Global $mainP  = GUICtrlCreatePic(@ScriptDir & "\" & $program & ".bmp", 1, 1, 498, 50)
Global $mainLV  = GUICtrlCreateListView("Date/Time|Path/File", 1, 53, 498, 148, $LVS_SORTDESCENDING, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
Global $mainG1  = GUICtrlCreateGroup("pScope",                  001, 200, 498, 158)
Global $mainG2  = GUICtrlCreateGroup("fScope | Port | Timer", 001, 243, 498, 115)
Global $mainG2  = GUICtrlCreateGroup("",            001, 286, 498, 072)
Global $mainI     = GUICtrlCreateInput($pScope, 13, 218, 450, 21)
Global $mainC1  = GUICtrlCreateCombo($fScope, 013, 261, 156, 021)
Global $mainC2  = GUICtrlCreateCombo($port,   172, 261, 156, 021)
Global $mainC3  = GUICtrlCreateCombo($timer,  331, 261, 156, 021)
Global $mainB1  = GUICtrlCreateButton(">",      466, 218, 021, 021, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB2  = GUICtrlCreateButton("START",  013, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB3  = GUICtrlCreateButton("STOP", 172, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainB4  = GUICtrlCreateButton("CLOSE",  331, 304, 156, 042, $BS_CENTER, $WS_EX_CLIENTEDGE)
Global $mainL     = GUICtrlCreateLabel("Freeware.", 1, 360, 498, 15, $SS_CENTER)
Global $Start

AutoItSetOption("GUIOnEventMode", 1); Opt
; AutoItSetOption("TrayIconHide", 1); Opt


HotKeySet("^{F1}", "start"); STRG + F1
HotKeySet("^{F2}", "dummy"); STRG + F2
HotKeySet("^{F3}", "close"); STRG + F3


GUICtrlSetBkColor($mainL, 0x484848)
GUICtrlSetColor($mainL, 0xFFFFFF)
GUICtrlSetFont($mainL, 8.5, 400)
GUICtrlSetOnEvent($mainB1, "path")
GUICtrlSetOnEvent($mainB2, "start")
GUICtrlSetOnEvent($mainB3, "stop")
GUICtrlSetOnEvent($mainB4, "close")
GUICtrlSetState($mainI, $GUI_DISABLE)
GUICtrlSetState($mainB2, $GUI_FOCUS)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState(@SW_SHOW, $mainGUI)


$fArray = StringSplit("*.csv;*.dat;*.doc;*.txt", ";", 0)
For $i = 1 to $fArray[0] Step 1
    If Not StringInStr($fArray[$i], $fScope, 0) Then
        GUICtrlSetData($mainC1, $fArray[$i], "")
    EndIf
Next
; <...>
$pArray = StringSplit("COM1:;COM2:;COM3:;COM4:;LPT1:;LPT2:;LPT3:;LPT4:", ";", 0)
For $i = 1 to $pArray[0] Step 1
    If Not StringInStr($pArray[$i], $port, 0) Then
        GUICtrlSetData($mainC2, $pArray[$i], "")
    EndIf
Next
; <...>
For $i = 1 to 30 Step 1
    GUICtrlSetData($mainC3, $i, "")
Next

While 1
    Sleep(1000)
    If $Start = 1 then _process()
WEnd

Func _Process()
        GUICtrlSetState($mainC1, $GUI_DISABLE)
        GUICtrlSetState($mainC2, $GUI_DISABLE)
        GUICtrlSetState($mainC3, $GUI_DISABLE)
        GUICtrlSetState($mainB1, $GUI_DISABLE)
        GUICtrlSetState($mainB2, $GUI_DISABLE)
        GUICtrlSetState($mainB3, $GUI_FOCUS)
; <...>
        If  StringIsASCII(GUICtrlRead($mainI))  = "" Or _
            StringIsASCII(GUICtrlRead($mainC1)) = "" Or _
            StringIsASCII(GUICtrlRead($mainC2)) = "" Or _
            StringIsInt(GUICtrlRead($mainC3))   = "" Then
; <...>
        Else
            For $i = 0 to 1 Step 1
                For $j = $timer to 1 Step -1
                    If $Start = 0 then Exitloop(2)
                    GUICtrlSetData($mainC3, $j, "")
                    Sleep(1000)
                Next
                $sDummy = FileFindFirstFile($pScope & "\" & $fScope)
                If $sDummy = -1 Then
                    $lCounter = $lCounter + 1
                    WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                    GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & "No file(s) found.", $mainLV)
                    lvAutosize()
                EndIf
                While 1
                    If $Start = 0 then Exitloop(1)
                    $fDummy = FileFindNextFile($sDummy)
                    If @error Then
                        ExitLoop
                    Else
                        $pCounter = $pCounter + 1
                        WinSetTitle($program, "", $program & " <Loop(s): " & $lCounter & " | Print(s): " & $pCounter & ">")
                        GUICtrlCreateListViewItem(StringRight(@YEAR, 2) & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & "|" & $pScope & "\" & $fDummy, $mainLV)
                        lvAutosize()
                    EndIf
                WEnd
                FileClose($sDummy)
                $i = 0
            Next
        EndIf
; <...>
        GUICtrlSetState($mainC1, $GUI_ENABLE)
        GUICtrlSetState($mainC2, $GUI_ENABLE)
        GUICtrlSetState($mainC3, $GUI_ENABLE)
        GUICtrlSetState($mainB1, $GUI_ENABLE)
        GUICtrlSetState($mainB2, $GUI_ENABLE)
        GUICtrlSetState($mainB2, $GUI_FOCUS)
EndFunc
    
Func path()
    $pDummy = FileSelectFolder("", "", 4, $pScope)
    If  $pDummy <> ""     And _
        Not StringInStr($pDummy, ".",   0) And _
        Not StringInStr($pDummy, "\\",  0)  Then
                $pScope = $pDummy
                GUICtrlSetData($mainI, $pScope)
    EndIf
EndFunc


Func start()
    $Start = 1
EndFunc


Func stop()
    $Start = 0
EndFunc


Func close()
    If StringIsASCII(GUICtrlRead($mainI)) Then
        RegWrite("HKCU\Software\" & $program, "pScope", "REG_SZ", GUICtrlRead($mainI))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC1)) Then
        RegWrite("HKCU\Software\" & $program, "fScope", "REG_SZ", GUICtrlRead($mainC1))
    EndIf
    If StringIsASCII(GUICtrlRead($mainC2)) Then
        RegWrite("HKCU\Software\" & $program, "Port",   "REG_SZ", GUICtrlRead($mainC2))
    EndIf
    If StringIsInt(GUICtrlRead($mainC3)) Then
        RegWrite("HKCU\Software\" & $program, "Timer",  "REG_SZ", GUICtrlRead($mainC3))
    EndIf
; <...>
    Exit
EndFunc


Func dummy()
EndFunc


Func lvAutosize()
    For $i = 0 to 1
        _GUICtrlListView_SetColumnWidth($mainLV, $i, $LVSCW_AUTOSIZE)
    Next
EndFunc

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.