Jump to content

abort or stoping a loop while file searching...


Recommended Posts

here's a part of my program(edited but fully working...). the full program runs fine, it's just that if during file searching and i've already found the files that i wanted, i cant seem to be able to stop the searching process, which may take sometime for it to ended.

i know i can use the Opt("GUIOnEventMode", 1) but i cant seem to find the right place to put it, in order to stop the process..

and i'm stuck here for few days.. (2 days actually, trying to pinpoint where is the right place to put it..), Really need your help guys..

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

HotKeySet("{ESC}", "pst")

$version = "My Program"
$GUI = GUICreate($version, 500, 300, 100, 100, -1, -1)
$inp1 = GUICtrlCreateListView("Filenames |Size(MB)|Type ", 10, 30, 480, 235, BitAND($LVS_SORTASCENDING, $LVS_SINGLESEL))
_GUICtrlListViewJustifyColumn($inp1, 1, 1)
_GUICtrlListViewJustifyColumn($inp1, 2, 2)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_TRACKSELECT, $LVS_EX_TRACKSELECT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
_GUICtrlListViewSetColumnWidth($inp1, 0, 350)
_GUICtrlListViewSetColumnWidth($inp1, 1, 60)
_GUICtrlListViewSetColumnWidth($inp1, 2, 50)
$tpp = GUICtrlCreateInput(".tmp", 15, 5, 40, 20)
$tpps = GUICtrlCreateButton("", 55, 5, 50, 20)
GUICtrlSetData($tpps, "Search")
$statuslabel = GUICtrlCreateLabel("", 5, 275, 490, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))
GUICtrlSetData($statuslabel, "Ready")

GUISetState()
While 1
    $Msg = GUIGetMsg()
    Sleep(50)
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            Exit
            
        Case $Msg = $tpps
            GUICtrlSetData($tpps, "Stop")
            $fold = FileSelectFolder("Browse for drive or folder to search... ", "")
            If @error Or $fold = "" Then
                GUICtrlSetData($tpps, "Search")
                ContinueLoop
            Else
                allt(StringTrimRight($fold, 1), GUICtrlRead($tpp))
            EndIf
    EndSelect
WEnd

Func allt($k, $ls)
    Dim $folders = _FileListToArray($k, "*.*", 2)
    If Not @error Then
        If $folders[0] <> 0 Then
            For $i = 1 To $folders[0]
                allt($k & "\" & $folders[$i], $ls)
            Next
        EndIf
    EndIf
    Dim $files = _FileListToArray($k, "*.*", 1)
    If Not @error Then
        If $files[0] <> 0 Then
            For $j = 1 To $files[0]
                $print = $k & "\" & $files[$j]
                Sleep(10)
                If StringRight($print, StringLen($ls)) = $ls Then
                    GUICtrlCreateListViewItem($print & "|" & Round(FileGetSize($print) / 1048576, 4) & "|" & StringUpper(StringRight($print, 3)), $inp1)
                EndIf
                ControlSetText($version, "", $statuslabel, "")
                ControlSetText($version, "", $statuslabel, "Searching in " & $print & "                                                                             ")
            Next
        EndIf
    EndIf
EndFunc   ;==>allt

Func pst()
    WinSetOnTop($version, "", 0)
    WinSetState($version, "", @SW_DISABLE)
    $ms = MsgBox(49, $version, "Program is currently paused...  " & @LF & "Press Ok to Continue, Cancel to Exit   ")
    If $ms = "2"  Then
        WinSetTitle($version, "", "Program is exiting, please wait...")
        Exit
    EndIf
    WinSetState($version, "", @SW_ENABLE)
    WinActivate($version, "")
    Return
EndFunc   ;==>pst
Link to comment
Share on other sites

here's a part of my program(edited but fully working...). the full program runs fine, it's just that if during file searching and i've already found the files that i wanted, i cant seem to be able to stop the searching process, which may take sometime for it to ended.

i know i can use the Opt("GUIOnEventMode", 1) but i cant seem to find the right place to put it, in order to stop the process..

and i'm stuck here for few days.. (2 days actually, trying to pinpoint where is the right place to put it..), Really need your help guys..

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

HotKeySet("{ESC}", "pst")

$version = "My Program"
$GUI = GUICreate($version, 500, 300, 100, 100, -1, -1)
$inp1 = GUICtrlCreateListView("Filenames |Size(MB)|Type ", 10, 30, 480, 235, BitAND($LVS_SORTASCENDING, $LVS_SINGLESEL))
_GUICtrlListViewJustifyColumn($inp1, 1, 1)
_GUICtrlListViewJustifyColumn($inp1, 2, 2)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_TRACKSELECT, $LVS_EX_TRACKSELECT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
_GUICtrlListViewSetColumnWidth($inp1, 0, 350)
_GUICtrlListViewSetColumnWidth($inp1, 1, 60)
_GUICtrlListViewSetColumnWidth($inp1, 2, 50)
$tpp = GUICtrlCreateInput(".tmp", 15, 5, 40, 20)
$tpps = GUICtrlCreateButton("", 55, 5, 50, 20)
GUICtrlSetData($tpps, "Search")
$statuslabel = GUICtrlCreateLabel("", 5, 275, 490, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))
GUICtrlSetData($statuslabel, "Ready")

GUISetState()
While 1
    $Msg = GUIGetMsg()
    Sleep(50)
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            Exit
            
        Case $Msg = $tpps
            GUICtrlSetData($tpps, "Stop")
            $fold = FileSelectFolder("Browse for drive or folder to search... ", "")
            If @error Or $fold = "" Then
                GUICtrlSetData($tpps, "Search")
                ContinueLoop
            Else
                allt(StringTrimRight($fold, 1), GUICtrlRead($tpp))
            EndIf
    EndSelect
WEnd

Func allt($k, $ls)
    Dim $folders = _FileListToArray($k, "*.*", 2)
    If Not @error Then
        If $folders[0] <> 0 Then
            For $i = 1 To $folders[0]
                allt($k & "\" & $folders[$i], $ls)
            Next
        EndIf
    EndIf
    Dim $files = _FileListToArray($k, "*.*", 1)
    If Not @error Then
        If $files[0] <> 0 Then
            For $j = 1 To $files[0]
                $print = $k & "\" & $files[$j]
                Sleep(10)
                If StringRight($print, StringLen($ls)) = $ls Then
                    GUICtrlCreateListViewItem($print & "|" & Round(FileGetSize($print) / 1048576, 4) & "|" & StringUpper(StringRight($print, 3)), $inp1)
                EndIf
                ControlSetText($version, "", $statuslabel, "")
                ControlSetText($version, "", $statuslabel, "Searching in " & $print & "                                                                             ")
            Next
        EndIf
    EndIf
EndFunc   ;==>allt

Func pst()
    WinSetOnTop($version, "", 0)
    WinSetState($version, "", @SW_DISABLE)
    $ms = MsgBox(49, $version, "Program is currently paused...  " & @LF & "Press Ok to Continue, Cancel to Exit   ")
    If $ms = "2"  Then
        WinSetTitle($version, "", "Program is exiting, please wait...")
        Exit
    EndIf
    WinSetState($version, "", @SW_ENABLE)
    WinActivate($version, "")
    Return
EndFunc   ;==>pst

If you're in a loop, for/next, while/wend, then you can get out of the loop at any time using ExitLoop. So you can have something like

For $n = 0 to 100

.

.

If $alldone then exitloop;get out of current loop

Next

You have to exit each nested loop as needed.

If you are in a function and there is nothing more to do you can use "Return" or "Return $Result", "If $condition then Return" etc.

Edited by martin
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

If you're in a loop, for/next, while/wend, then you can get out of the loop at any time using ExitLoop. So you can have something like

For $n = 0 to 100

.

.

If $alldone then exitloop;get out of current loop

Next

You have to exit each nested loop as needed.

i cannot use ExitLoop in For/Next except for While/Wend. error saying " Cannot use ExitLoop outside of a loop" - something like that..

If you are in a function and there is nothing more to do you can use "Return" or "Return $Result", "If $condition then Return" etc.

my function will exit if there's nothing more to do.. nothing wrong with that...

the problem is here:

Func allt($k, $ls)
    Dim $folders = _FileListToArray($k, "*.*", 2)
    If Not @error Then
        If $folders[0] <> 0 Then
            For $i = 1 To $folders[0]
                allt($k & "\" & $folders[$i], $ls)
            Next
        EndIf
    EndIf
    Dim $files = _FileListToArray($k, "*.*", 1)
    If Not @error Then
        If $files[0] <> 0 Then
            For $j = 1 To $files[0]
                $print = $k & "\" & $files[$j]
                Sleep(10)
                If $stop = "true" Then
                    exit       ;if i put this, the program will exit, not stopped so i cannot get the results
                ElseIf StringRight($print, StringLen($ls)) = $ls Then
                    GUICtrlCreateListViewItem($print & "|" & Round(FileGetSize($print) / 1048576, 4) & "|" & StringUpper(StringRight($print, 3)), $inp1)
                EndIf
                ControlSetText($version, "", $statuslabel, "")
                ControlSetText($version, "", $statuslabel, "Searching in " & $print & "                                                                             ")
            Next
        EndIf
    EndIf
EndFunc   ;==>allt
Edited by mikiutama
Link to comment
Share on other sites

mikiutama

Hi! Sorry, i don`t have a more time for detail writing a script, but i just want show example to you.

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

HotKeySet("{ESC}", "pst")

$version = "My Program"
$GUI = GUICreate($version, 500, 300, 100, 100, -1, -1)
$inp1 = GUICtrlCreateListView("Filenames |Size(MB)|Type ", 10, 30, 480, 235, BitAND($LVS_SORTASCENDING, $LVS_SINGLESEL))
_GUICtrlListView_JustifyColumn($inp1, 1, 1)
_GUICtrlListView_JustifyColumn($inp1, 2, 2)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_TRACKSELECT, $LVS_EX_TRACKSELECT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
_GUICtrlListView_SetColumnWidth($inp1, 0, 350)
_GUICtrlListView_SetColumnWidth($inp1, 1, 60)
_GUICtrlListView_SetColumnWidth($inp1, 2, 50)

$tpp = GUICtrlCreateInput(".tmp", 15, 5, 40, 20)

$tpps = GUICtrlCreateButton("Search", 55, 5, 50, 20)

$statuslabel = GUICtrlCreateLabel("", 5, 275, 490, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

GUICtrlSetData($statuslabel, "Ready")

GUISetState()
While 1
    $Msg = GUIGetMsg()
    Sleep(50)
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            Exit
            
        Case $Msg = $tpps
            GUICtrlSetData($tpps, "Stop")
            $fold = FileSelectFolder("Browse for drive or folder to search... ", "")
            If @error Or $fold = "" Then
                GUICtrlSetData($tpps, "Search")
                ContinueLoop
            Else
                allt($fold, GUICtrlRead($tpp))
            EndIf
    EndSelect
WEnd

Func allt($iFolder, $iExt)
    Local $aFiles, $iFullPath, $iRet
    
    $aFiles = _FileListToArray($iFolder, "*.*", 0)
    If @error Then Return 0

    For $i = 1 To $aFiles[0]
        $iFullPath = $iFolder &"\"& $aFiles[$i]
        If StringInStr(FileGetAttrib($iFullPath), "D") Then
            $iRet = allt($iFullPath, $iExt)
        ElseIf StringRight($iFullPath, 4) = $iExt Then
            GUICtrlCreateListViewItem($iFullPath & "|" & Round(FileGetSize($iFullPath) / 1048576, 4) & "|" & StringUpper(StringRight($iFullPath, 3)), $inp1)
            ControlSetText($version, "", $statuslabel, "Searching in " & $iFullPath & "   ")
        EndIf
    Next
    GUICtrlSetData($statuslabel, "Ready")
EndFunc   ;==>allt

Func pst()
    WinSetOnTop($version, "", 0)
    WinSetState($version, "", @SW_DISABLE)
    $ms = MsgBox(49, $version, "Program is currently paused...  " & @LF & "Press Ok to Continue, Cancel to Exit   ")
    If $ms = "2"  Then
        WinSetTitle($version, "", "Program is exiting, please wait...")
        Exit
    EndIf
    WinSetState($version, "", @SW_ENABLE)
    WinActivate($version, "")
    Return
EndFunc   ;==>pst

:)

Edited by rasim
Link to comment
Share on other sites

Tweaked to show usage of GuiOnEventMode, and corrected for outdated GuiListView* vice _GuiCtrlListView_* functions. Note the use of the text in the Search/Stop button as a run flag:

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <File.au3>

HotKeySet("{ESC}", "pst")

Opt("GuiOnEventMode", 1)

Global $version = "My Program", $GUI, $inp1, $hInp1, $tpp, $tpps, $statuslabel, $fold

$GUI = GUICreate($version, 500, 300, 100, 100, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

$inp1 = GUICtrlCreateListView("Filenames |Size(MB)|Type ", 10, 30, 480, 235, BitAND($LVS_SORTASCENDING, $LVS_SINGLESEL))
$hInp1 = ControlGetHandle($GUI, "", $inp1)
_GUICtrlListView_JustifyColumn($hInp1, 1, 1)
_GUICtrlListView_JustifyColumn($hInp1, 2, 2)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlSendMsg($inp1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_TRACKSELECT, $LVS_EX_TRACKSELECT)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
_GUICtrlListView_SetColumnWidth($hInp1, 0, 350)
_GUICtrlListView_SetColumnWidth($hInp1, 1, 60)
_GUICtrlListView_SetColumnWidth($hInp1, 2, 50)

$tpp = GUICtrlCreateInput("*.tmp", 15, 5, 40, 20)

$tpps = GUICtrlCreateButton("", 55, 5, 50, 20)
GUICtrlSetOnEvent(-1, "_ButtonHit")
GUICtrlSetData(-1, "Search")

$statuslabel = GUICtrlCreateLabel("", 5, 275, 490, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))
GUICtrlSetData(-1, "Ready")

GUISetState()

While 1
    Sleep(20)
    If GUICtrlRead($tpps) = "Stop" Then
        $fold = FileSelectFolder("Browse for drive or folder to search... ", "")
        $filter = GUICtrlRead($tpp)
        allt($fold, $filter)
        GUICtrlSetData($tpps, "Search")
    EndIf
WEnd

Func _ButtonHit()
    Switch GUICtrlRead($tpps)
        Case "Search"
            GUICtrlSetData($tpps, "Stop")
        Case "Stop"
            GUICtrlSetData($tpps, "Search")
    EndSwitch
EndFunc   ;==>_ButtonHit

Func allt($k, $ls)
    Local $folders, $files, $print
    Local $folders = _FileListToArray($k, "*.*", 2)
    If Not @error Then
        If $folders[0] <> 0 Then
            For $i = 1 To $folders[0]
                ; Exit function if user clicked "Stop"
                If GUICtrlRead($tpps) = "Search" Then Return
                
                allt($k & "\" & $folders[$i], $ls)
            Next
        EndIf
    EndIf
    $files = _FileListToArray($k, $ls, 1)
    If Not @error Then
        If $files[0] <> 0 Then
            For $j = 1 To $files[0]
                ; Exit function if user clicked "Stop"
                If GUICtrlRead($tpps) = "Search" Then Return
                
                $print = $k & "\" & $files[$j]
                GUICtrlCreateListViewItem($print & "|" & Round(FileGetSize($print) / 1048576, 4) & "|" & StringUpper(StringRight($print, 3)), $inp1)
                ControlSetText($version, "", $statuslabel, "")
                ControlSetText($version, "", $statuslabel, "Searching in " & $print & "                                                                             ")
            Next
        EndIf
    EndIf
EndFunc   ;==>allt

Func pst()
    WinSetOnTop($version, "", 0)
    WinSetState($version, "", @SW_DISABLE)
    Local $ms = MsgBox(49, $version, "Program is currently paused...  " & @LF & "Press Ok to Continue, Cancel to Exit   ")
    If $ms = "2" Then
        WinSetTitle($version, "", "Program is exiting, please wait...")
        Exit
    EndIf
    WinSetState($version, "", @SW_ENABLE)
    WinActivate($version, "")
    Return
EndFunc   ;==>pst

Func _Quit()
    Exit
EndFunc   ;==>_Quit

:)

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

  • 2 months later...

@ PsaltyDS:

Just want to say thank you :D Your exampe is EXACTLY the solution i was looking for!!!!

works perfect!!!!! since two days i was searching for a solution howto stop an active loop.

before, i had every button as "case $msg = $button" i had to rewrite it a little bit.

Greetings from Germany, Andy

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