Okay, so I overdid it a little bit, but then you'll see what we can do with the threads.
A progress bar slows down the management of the rest of the program considerably, another solution is to update it only from time to time.
Full tested sample.
Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_Thread = #False
MessageRequester("Warning !!",
"You must enable ThreadSafe support in compiler options",
#PB_MessageRequester_Ok )
End
CompilerEndIf
; XIncludeFile "Search_Error.pbf"
; Copy/past from form designer to do one single file for forum
; ------------------------------------------------------------------------------------
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;
Enumeration FormWindow
#Window_0
EndEnumeration
Enumeration FormGadget
#Lst_Errors
#Btn_Open
#Str_Search
#Btn_Search
#Btn_Stop
#Btn_Quit
EndEnumeration
Enumeration FormFont
#Font_Window_0_0
EndEnumeration
LoadFont(#Font_Window_0_0,"Consolas", 10)
Declare ResizeGadgetsWindow_0()
Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
OpenWindow(#Window_0, x, y, width, height, "Show Errors", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
CreateStatusBar(0, WindowID(#Window_0))
AddStatusBarField(200)
StatusBarProgress(0, 0, 0)
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 1, "Ready")
ListIconGadget(#Lst_Errors, 10, 10, 580, 330, "Line", 60)
AddGadgetColumn(#Lst_Errors, 1, "Text", 500)
SetGadgetFont(#Lst_Errors, FontID(#Font_Window_0_0))
ButtonGadget(#Btn_Open, 10, 345, 100, 25, "Open...")
StringGadget(#Str_Search, 240, 345, 100, 25, "Error")
ButtonGadget(#Btn_Search, 135, 345, 100, 25, "Search for")
DisableGadget(#Btn_Search, 1)
ButtonGadget(#Btn_Stop, 345, 345, 100, 25, "Stop !")
ButtonGadget(#Btn_Quit, 490, 345, 100, 25, "Quit")
EndProcedure
Procedure ResizeGadgetsWindow_0()
Protected FormWindowWidth, FormWindowHeight
FormWindowWidth = WindowWidth(#Window_0)
FormWindowHeight = WindowHeight(#Window_0)
ResizeGadget(#Lst_Errors, 10, 10, FormWindowWidth - 20, FormWindowHeight - StatusBarHeight(0) - 47)
ResizeGadget(#Btn_Open, 10, FormWindowHeight - 55, 100, 25)
ResizeGadget(#Str_Search, 240, FormWindowHeight - 55, 100, 25)
ResizeGadget(#Btn_Search, 135, FormWindowHeight - 55, 100, 25)
ResizeGadget(#Btn_Stop, 345, FormWindowHeight - 55, 100, 25)
ResizeGadget(#Btn_Quit, FormWindowWidth - 110, FormWindowHeight - 55, 100, 25)
EndProcedure
; ------------------------------------------------------------------------------------
Global FileName$
Global Nb_Lines.l
Global Txt2Find$
Global ID_Thread
Declare Search_Txt(*Value)
Procedure Show_Infos()
Nb_Lines = FileSize(FileName$)
StatusBarText(0, 1, FormatNumber(Nb_Lines, 0, " ", " ") + " bytes")
EndProcedure
; Here we go...
OpenWindow_0()
; If a filename is provide as parameter or drop on icon, use it directly
If CountProgramParameters() = 1
FileName$ = ProgramParameter(0)
Show_Infos()
SetWindowTitle(#Window_0, FileName$)
Txt2Find$ = GetGadgetText(#Str_Search)
If Txt2Find$ <> ""
ID_Thread = CreateThread(@Search_Txt(), 1)
EndIf
EndIf
; Thread for searching
Procedure Search_Txt(*Value)
DisableGadget(#Btn_Stop, 0)
StatusBarText(0, 1, "Searching...")
Protected hFile
Protected i
Protected Tmp_Line$
Protected Lines
ReadFile(hFile, FileName$)
; Count line (EOL)
While Not Eof(hFile)
ReadString(hFile)
Lines + 1
Wend
CloseFile(hFile)
; Update 20 x per file whatever filesize
Protected Factor = Lines / 20
ReadFile(hFile, FileName$)
While Not Eof(hFile)
i + 1
If i / Factor = i % Factor
StatusBarText(0, 1, Str(i) + " / " + Lines)
StatusBarProgress(0, 0, i, #PB_StatusBar_Raised, 0, Lines)
EndIf
Tmp_Line$ = ReadString(hFile)
If FindString(Tmp_Line$, Txt2Find$) > 0
AddGadgetItem(#Lst_Errors, -1, Str(i) + Chr(10) + Tmp_Line$)
Tmp_Line$ = ""
EndIf
Wend
CloseFile(hFile)
StatusBarProgress(0, 0, i)
StatusBarText(0, 1, "OK, Search ended")
EndProcedure
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
; Thread will be killed when program quit,
; so whatever If still running...
End
Case #PB_Event_SizeWindow
; Auto generated by form designer
; when change at list on lock gadget position
ResizeGadgetsWindow_0()
Case #PB_Event_Gadget
Select EventGadget()
Case #Btn_Open
FileName$ = OpenFileRequester("Log file to analyse", "", "", 0)
If FileName$ <> ""
DisableGadget(#Btn_Search, 0)
Txt2Find$ = GetGadgetText(#Str_Search)
Show_Infos()
EndIf
Case #Btn_Search
ClearGadgetItems(#Lst_Errors)
ID_Thread = CreateThread(@Search_Txt(), 1)
Case #Btn_Stop
; Bad way, but sometime needed
If IsThread(ID_Thread)
KillThread(ID_Thread)
StatusBarText(0, 1, "Arglll, you have killed my thread |-/")
EndIf
Case #Btn_Quit
End
EndSelect
EndSelect
ForEver
End
actual procedure take care of number of line and update 20 time whatever number of line