Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stop/pause/continue subfunction
#1
How can I stop/pause/continue the below dialog?

When "START REMOVE" is clicked each item from the listbox is automatically removed from top to bottom.
For that to happen subfunction "remove_item_and_process" is called.

I want to: stop and pause/continue.
"pause/continue" is toggle based, meaning press "pause/continue" it pauses, click button again and it continues.

I found on the forum a way to end (stop) a subfunction but I couldn't get it to work.
And I do not know how to approach the "pause/continue"

The only method I can think of is using global variables, what I mean is when "STOP" is pressed the global variable is set to a certian value.
I then use this in the subfunction to check for. I might also be able to use this for the "PAUSE/CONTINUE"(?)
The downside is, is that have to check for that value after every command.
I hope a better more efficient method exists?



Function ListDialogRotate2
Code:
Copy      Help
str- appname;appname="ListDialog Rotator"
str- lb_items=
;item 1
;item 2
;item 3
;item 4
;item 5
;item 6
;item 7

str dd=
F
;BEGIN DIALOG
;0 "" 0x90CC0AC8 0x0 0 0 298 136 "{appname}" "4"
;3 ListBox 0x54230901 0x200 0 0 296 114 ""
;21 Button 0x54032000 0x0 5 120 78 14 "START REMOVE" "From top, start removing items one by one[][SHIFT] + left click = starts from BOTTOM"
;23 Button 0x54032000 0x4 95 120 32 14 "REFILL"
;4 Button 0x54032000 0x0 135 120 48 14 "stop"
;5 Button 0x54032000 0x0 195 120 72 14 "pause/continue"
;END DIALOG
;DIALOG EDITOR: "" 0x2040A00 "*" "" "" ""

str controls = "3"
str lb3
if(!ShowDialog(dd &sub.DlgProc &controls)) ret


#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG        
,,double- demo_wait_time;demo_wait_time=1
,,str- lb_items
,,sub.fill_lb(hDlg 3 lb_items)
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 21 ;; START REMOVING ITEMS FROM LISTBOX
,,mac "sub.remove_item_and_process" "" id(3 hDlg) demo_wait_time
,
,case 23 ;; REFILL THE LISTBOX
,,sub.fill_lb(hDlg 3 lb_items)
,
,case 4 ;; STOP (this does not work)
,,int- hThread;hThread=mac("sub.remove_item_and_process" "" hDlg)
,,EndThread("" hThread)
,,outt "Stopped?"

,case 5 ;; PAUSE/CONTINUE: ???
,,outt F"pause/continue clicked..."
ret 1


#sub remove_item_and_process
function cid  double'demo_wait_time

int amount i


amount=LB_GetCount(cid)
if(amount=0)
,mes(F"No content to process" F"test" "!")
,ret
rep amount
,LB_GetItemText(cid 0 _s)
,LB_SelectItem(cid 0 1)
,outt F"PROCESSING, {_s}, {i} -> ({i} index listbox content)"
,wait demo_wait_time
,SendMessage(cid LB_DELETESTRING 0 0)
,outt F"PROCESSED, REMOVED: {_s}, {i} -> ({i} index listbox content)"
,out "-"
,i=i+1

mes(F"Finished[]No more content to process" F"test" "i")


#sub fill_lb
function int'hDlg int'lb_id str'lb_items

SendMessage(id(lb_id hDlg) LB_RESETCONTENT 0 0) ;; clear first then refill

int cnt
foreach _s lb_items
,LB_Add id(lb_id hDlg) _s cnt
,cnt=cnt+1
#2
your stop wasnt working because you were running a new sub func and ending that thread. Need to get handle when you start sub func.

here is working start stop pause resume

Function ListDialogRotate2a
Code:
Copy      Help
str lb_items=
;item 1
;item 2
;item 3
;item 4
;item 5
;item 6
;item 7

str dd=
F
;BEGIN DIALOG
;0 "" 0x90CC0AC8 0x0 0 0 298 136 "{appname}" "4"
;3 ListBox 0x54230901 0x200 0 0 296 114 ""
;21 Button 0x54032000 0x0 5 120 78 14 "START REMOVE" "From top, start removing items one by one[][SHIFT] + left click = starts from BOTTOM"
;23 Button 0x54032000 0x4 95 120 32 14 "REFILL"
;4 Button 0x54032000 0x0 135 120 48 14 "stop"
;5 Button 0x5C012000 0x0 195 120 72 14 "pause/continue"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C00 "*" "" "" ""

str controls = "3"
str lb3
if(!ShowDialog(dd &sub.DlgProc &controls)) ret


#sub DlgProc v
function# hDlg message wParam lParam
double demo_wait_time=1.0
sel message
,case WM_INITDIALOG
,,int- hThread
,,sub.fill_lb(id(3 hDlg) lb_items)
,case WM_DESTROY
,RemoveProp(id(3 hDlg) "PauseResume") 
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 21 ;; START REMOVING ITEMS FROM LISTBOX
,if(!GetProp(id(3 hDlg) "PauseResume"))
,,,hThread=    mac("sub.remove_item_and_process" "" id(3 hDlg) demo_wait_time hDlg)
,,,TO_Enable(hDlg "5" 1)
,case 23 ;; REFILL THE LISTBOX
,,sub.fill_lb(id(3 hDlg) lb_items)
,case 4 ;; STOP
,,EndThread("" hThread)
,,outt "Stopped?"
,,hThread=!hThread
,,SetProp(id(3 hDlg) "PauseResume" 0)
,,TO_Enable(hDlg "5" 0)
,case 5 ;; PAUSE/CONTINUE: ???
,if(!GetProp(id(3 hDlg) "PauseResume"))
,,SetProp(id(3 hDlg) "PauseResume" 1)
,,outt F"paused..."
,else
,,SetProp(id(3 hDlg) "PauseResume" 0)
,,outt F"continued..."
ret 1

#sub remove_item_and_process
function cid  double'demo_wait_time hdlg
int amount i
amount=LB_GetCount(cid)
if(amount=0)
,mes(F"No content to process" F"test" "!")
,ret
rep amount
,if(GetProp(cid "PauseResume"))
,,rep
,,,if(!GetProp(cid "PauseResume"))
,,,,break
,,,0.1
,LB_GetItemText(cid 0 _s)
,LB_SelectItem(cid 0 1)
,outt F"PROCESSING, {_s}, {i} -> ({i} index listbox content)"
,wait demo_wait_time
,SendMessage(cid LB_DELETESTRING 0 0)
,outt F"PROCESSED, REMOVED: {_s}, {i} -> ({i} index listbox content)"
,out "-"
,i=i+1
SetProp(cid "PauseResume" 0)
TO_Enable(hdlg "5" 0)
mes(F"Finished[]No more content to process" F"test" "i")

#sub fill_lb
function cid str'lb_items

SendMessage(cid LB_RESETCONTENT 0 0) ;; clear first then refill

int cnt
foreach _s lb_items
,LB_Add cid _s cnt
,cnt=cnt+1
#3
wow!
Thank you!!!
#4
After looking at this again i came up with a better version removed one button, got rid of the setprop/getprop/removeprop as that way of pausing had some limits.. now the buttons are dynamic and disable and enable depending on what mode your in. So when thread is paused can only resume. when list removal is complete can only refill ect ect.. also got rid of uneeded thread variables when not needed.

Function ListDialogRotate2b
Code:
Copy      Help
str appname="ListDialog Rotator"
str lb_items=
;item 1
;item 2
;item 3
;item 4
;item 5
;item 6
;item 7

str dd=
F
;BEGIN DIALOG
;0 "" 0x90CC0AC8 0x0 0 0 298 136 "{appname}" "4"
;3 ListBox 0x54230901 0x200 0 0 296 114 ""
;21 Button 0x54011F03 0x0 17 120 78 14 "START REMOVE" "From top, start removing items one by one[][SHIFT] + left click = starts from BOTTOM"
;23 Button 0x54037000 0x4 132 120 32 14 "REFILL"
;5 Button 0x54017003 0x0 202 120 78 14 "PAUSE"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C00 "*" "" "" ""

str controls = "3 21 5"
str lb3 c21STA c5PAU
if(!ShowDialog(dd &sub.DlgProc &controls)) ret

#sub DlgProc v
function# hDlg message wParam lParam
double demo_wait_time=1.0
sel message
,case WM_INITDIALOG
,int- hThread
,sub.fill_lb(id(3 hDlg) lb_items)
,TO_Enable(hDlg "5 23" 0)
,case WM_DESTROY
,case WM_CLOSE
,if(hThread)
,,if !IsWindowEnabled(id(21 hDlg))
,,,out "Thread is paused ,Cannot close dialog"
,,,;but id(5 hDlg);; uncomment if your want to resume thread automatically
,,,ret 1
,,else
,,,out "Thread is running, cannot close dialog"
,,,ret 1
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 21 ;; START REMOVING ITEMS FROM LISTBOX
,if(but(lParam)) ;;checked
,,hThread=    mac("sub.remove_item_and_process" "" id(3 hDlg) demo_wait_time hDlg)
,,TO_Enable(hDlg "5" 1)
,,TO_Enable(hDlg "23" 0)
,,_s="STOP" ;
,else
,,EndThread("" hThread)
,,hThread=!hThread
,,but- id(5 hDlg)
,,TO_Enable(hDlg "5" 0)
,,TO_Enable(hDlg "23" 1)
,,_s="START REMOVE"
,_s.setwintext(lParam)
,case 23 ;; REFILL THE LISTBOX
,,sub.fill_lb(id(3 hDlg) lb_items)
,,TO_Enable(hDlg "21" 1)
,,TO_Enable(hDlg "23" 0)
,case 5 ;; PAUSE/CONTINUE: ???
,if(but(lParam)) ;;checked
,,TO_Enable(hDlg "21" 0)    
,,SuspendThread hThread
,,_s="CONTINUE"
,else
,,TO_Enable(hDlg "21" 1)    
,,ResumeThread hThread
,,_s="PAUSE"
,_s.setwintext(lParam)
ret 1

#sub remove_item_and_process
function cid  double'demo_wait_time hdlg
int amount i
amount=LB_GetCount(cid)
if(amount=0)
,mes(F"No content to process" F"test" "!")
,but- id(21 hdlg)
,TO_Enable(hdlg "21" 0)
,ret
rep amount
,LB_GetItemText(cid 0 _s)
,LB_SelectItem(cid 0 1)
,outt F"PROCESSING, {_s}, {i} -> ({i} index listbox content)"
,wait demo_wait_time
,SendMessage(cid LB_DELETESTRING 0 0)
,outt F"PROCESSED, REMOVED: {_s}, {i} -> ({i} index listbox content)"
,out "-"
,i=i+1
mes(F"Finished[]No more content to process" F"test" "i")
but- id(21 hdlg)
TO_Enable(hdlg "5 21" 0)
TO_Enable(hdlg "23" 1)

#sub fill_lb
function cid str'lb_items

SendMessage(cid LB_RESETCONTENT 0 0) ;; clear first then refill
foreach _s lb_items
,LB_Add cid _s
#5
Thank you!
Really appreciate this!
#6
Added one more safety feature so dialog cannot be closed while thread is running or paused. It is needed because qm doesn't know about paused threads and threads need to be resumed before dialog closes.
fixed in post#4 ListDialogRotate2b
#7
Thanks!


Forum Jump:


Users browsing this thread: 1 Guest(s)