Progress Bar Threaded
Posted: Wed Jul 21, 2010 5:09 pm
My progress bar does not update properly. I read on the blog that it needed to be threaded.
Not sure how to update the progress bar from the main window? Around lines 42 - 63
Chopped this down from another program so this example may be more complex than it needs to be.
TIA
Not sure how to update the progress bar from the main window? Around lines 42 - 63
Chopped this down from another program so this example may be more complex than it needs to be.
TIA
Code: Select all
; ------------------------------------------------------------
; progress bar example
; ------------------------------------------------------------
; ------------------------------------------------------------
; variables
; ------------------------------------------------------------
Global eWaitWindow.i ;;
Global eType.i ;;
Global gActiveGadget.i ;;
Global progressBarWindow.i = 0 ;;
Global gProgressBarGadget.i = 0 ;;
Global gProgressBarGadgetLevel.i = 50 ;;
Global gDoneYet.i = 0 ;;
;------------------------------------------------------------
; init a main window
;------------------------------------------------------------
Global gWinMain = OpenWindow( #PB_Any, 0, 0, 480, 320, ;; create a window get windowID into mainWin
_ "winMain", ;; title
_ #PB_Window_SystemMenu | ;; has system menu
_ #PB_Window_SizeGadget | ;; allow resize
_ #PB_Window_MinimizeGadget | ;; has minimize button
_ #PB_Window_MaximizeGadget | ;; has maximize button
_ #PB_Window_ScreenCentered) ;; center the screen
;------------------------------------------------------------
; two buttons
;------------------------------------------------------------
Global gBtnPrev = ButtonGadget(#PB_Any, 305, 10, 80, 24, "DEC") ;; DEC
Global gBtnNext = ButtonGadget(#PB_Any, 390, 10, 80, 24, "INC") ;; INC
;------------------------------------------------------------
; a field
;------------------------------------------------------------
Global gFieldValue = StringGadget(#PB_Any, 40, 40, 320, 24,"") ;; see the current value
;------------------------------------------------------------
; progress bar
;------------------------------------------------------------
Procedure progressBar(gDoneYet) ;; this will be threaded
If OpenWindow(0, 0, 0, 320, 20, "Progress ...", ;; main window for progress bar
_ #PB_Window_SystemMenu | ;;
_ #PB_Window_ScreenCentered | ;;
_ #PB_Window_Tool | ;;
_ #PB_Window_BorderLess) ;;
gProgressBarGadget = ProgressBarGadget(#PB_Any, ;; progress bar
_ 0,0,320,20,0,100, ;;
_ #PB_Window_WindowCentered) ;;
Repeat ;; loop begin
SetGadgetState(gProgressBarGadget, gProgressBarGadgetLevel) ;; set the state
Delay (250) ;; fast enough
Until WaitWindowEvent()=#PB_Event_CloseWindow ;; close option
;; ********************************************************************************************************
;; must process events here???
;; how do I get the window to automatically update progress bar??
;; ********************************************************************************************************
EndIf ;; end main window of progressbar
EndProcedure ;; end procedure for progressbar
Thread = CreateThread(@progressBar(), 0) ;; start the procedure
;------------------------------------------------------------
; mouse navigation
;------------------------------------------------------------
Procedure MouseNavigationLeft()
;-----------------------------------------------------------
; [Left Mouse Button] clicked
; find which gadget and take action
;-----------------------------------------------------------
If gWin_kb_MouseButtonLeft > -1 And _ ;; if mouse button is clicked
EType = 0 ;; mouseUp
Select (gActiveGadget) ;; which gadget sent a left click
;-----------------------------------------------------------
; [Left Mouse] click on Prev card button
;-----------------------------------------------------------
Case gBtnPrev ;; prev Button clicked
If gProgressBarGadgetLevel > 1 ;; if level is not zero
gProgressBarGadgetLevel = gProgressBarGadgetLevel - 10 ;; subtract 10
SetGadgetText(gFieldValue, Str(gProgressBarGadgetLevel)) ;; show number in field
EndIf ;; end if prev button clicked
;-----------------------------------------------------------
; [Left Mouse] click on Next card button
;-----------------------------------------------------------
Case gBtnNext ;; next Button clicked
If gProgressBarGadgetLevel < 100 ;; if level is not 100
gProgressBarGadgetLevel = gProgressBarGadgetLevel + 10 ;; add 10
SetGadgetText(gFieldValue, Str(gProgressBarGadgetLevel)) ;; show number in field
EndIf ;; end if next button clicked
EndSelect ;; end select gadget from left click
EndIf ;; end if mouse button clicked
EndProcedure ;; end procedure mouse navigation left
;------------------------------------------------------------
; event loop
;------------------------------------------------------------
Procedure EventLoop()
;-----------------------------------------------------------
; event loop
;-----------------------------------------------------------
Repeat ;; run the loop until QUIT = 1
;Debug (eWaitWindow.i) ;;
;Debug (eType.i) ;;
;Debug (gActiveGadget.i) ;;
gWin_kb_MouseButtonLeft_LastState = gWin_kb_MouseButtonLeft ;; grab last state of left mouse key
gWin_kb_MouseButtonRight_LastState = gWin_kb_MouseButtonRight ;; grab last state of right mouse button
gActiveGadgetLast = gActiveGadget ;; grab (last) gadget that has focus
;; ---------------------------------------------------------
;; grab new needed key states
;; ---------------------------------------------------------
gWin_kb_MouseButtonLeft = GetAsyncKeyState_(#VK_LBUTTON) ;; left mouse button
gWin_kb_MouseButtonRight = GetAsyncKeyState_(#VK_RBUTTON) ;; right mouse button
;;---------------------------------------------------------
;; grab new current events
;;---------------------------------------------------------
eWaitWindow = WaitWindowEvent() ;; get event ID (used for resizing)
eType = EventType() ;; looking for mouse clicks i.e. left click
gActiveGadget = GetActiveGadget() ;; the gadget that has the current focus
;;-----------------------------------------------------------
;; Check mouse
;;-----------------------------------------------------------
MouseNavigationLeft() ;; check mouse
;;-----------------------------------------------------------
;; Window Events
;;-----------------------------------------------------------
Select eWaitWindow ;; (used for resizing, quitting, return key)
Case #PB_Event_SizeWindow ;; we were resized
Case #PB_Event_CloseWindow ;; If the user has pressed on the close button
Quit = 1 ;; get ready to quit the app
EndSelect ;; end of select myEvent
Until Quit = 1 ;; quit is one so we can close all windows
;;----------------------------------------------------------
;; end event loop
;;----------------------------------------------------------
EndProcedure
;;------------------------------------------------------------
;; main Program
;;------------------------------------------------------------
EventLoop() ;; done - event driven
End ;; done - End Program (close any still open windows)