Threads For Dummies
Posted: Thu Jun 09, 2011 6:19 am
Having just started using threads, it took me a few hours of brain storming to work out what I think is a working thread for programs that require a task to run in the background such as copying files using CopyFile and have another task being a progress window at the same time the main program is running.
The submitted code is a working example of what I did for my MP3 player code to copy files in the background, update a progress window and also be able to continue using the player as usual. I gave it a severe testing the other night while having a couple of wines or three, (well I was feeling a little bit under the weather next morning) and I was impressed at how well it worked, copying hundreds of MP3 files in the background, while I was listening to music and using the other functions of the player at the same time and not have any noticeable glitches.
So to get to the point of this posting, I was wondering if anyone was interested in contributing to it, especially the Gurus of PB coding, pointing out to us dummies of threads, what pit falls to look out for and best syntax practices, without getting too complicated, especially considering tearaway threads could be a potential danger to people's operating systems, and this post also be a helpful tutorial on threads for beginners to PB.
The following code is a working example of what I made up for my MP3 player code to copy files in the background and be a starting point for critical analysis, such as should I use Mutex, or you should put this check in for better security etc.
I think that I have covered most bases to prevent a severe crash, but not absolutely sure if there are any problems that I have not thought of that could crash the program, lock up the computer and in the worst case corrupt the operating system in some way.
I still need to tidy up the way DisableGadget(#btn_Main,0) is called for COPYING COMPLETE message delay and have the button fade back in synchronization. But I have prevented the user entering back into the procedure before it had actually ended with ProgressFlag=0 check.
Some other good sources for reading on the use of threads...
PureBasic Help Documentation
Survival Guide by blueznl
PureBasic - A Beginners Guide by Kale
The submitted code is a working example of what I did for my MP3 player code to copy files in the background, update a progress window and also be able to continue using the player as usual. I gave it a severe testing the other night while having a couple of wines or three, (well I was feeling a little bit under the weather next morning) and I was impressed at how well it worked, copying hundreds of MP3 files in the background, while I was listening to music and using the other functions of the player at the same time and not have any noticeable glitches.
So to get to the point of this posting, I was wondering if anyone was interested in contributing to it, especially the Gurus of PB coding, pointing out to us dummies of threads, what pit falls to look out for and best syntax practices, without getting too complicated, especially considering tearaway threads could be a potential danger to people's operating systems, and this post also be a helpful tutorial on threads for beginners to PB.
The following code is a working example of what I made up for my MP3 player code to copy files in the background and be a starting point for critical analysis, such as should I use Mutex, or you should put this check in for better security etc.
I think that I have covered most bases to prevent a severe crash, but not absolutely sure if there are any problems that I have not thought of that could crash the program, lock up the computer and in the worst case corrupt the operating system in some way.
I still need to tidy up the way DisableGadget(#btn_Main,0) is called for COPYING COMPLETE message delay and have the button fade back in synchronization. But I have prevented the user entering back into the procedure before it had actually ended with ProgressFlag=0 check.
Code: Select all
Enumeration
#win_Main
#win_Progress
EndEnumeration
Enumeration
#txt_Progress
#btn_Cancel
#btn_Main
EndEnumeration
Global Item,Count,ProgressFlag,TaskFlag,Cancel
Procedure ProgressWindow(*Dummy)
If OpenWindow(#win_Progress,0,0,300,60,"File Copy In Progress",#PB_Window_ScreenCentered)
TextGadget(#txt_Progress,10,10,280,15,"",#PB_Text_Center)
ButtonGadget(#btn_Cancel,120,35,60,20,"Cancel")
Temp=-1 ;Make sure Temp <> Item
Repeat
Event=WaitWindowEvent(1)
If Event=#PB_Event_Gadget And EventGadget()=#btn_Cancel ;Cancel out of the task
Cancel=1:Break
EndIf
If Temp<>Item ;Prevents flickering of the progress text
Temp=Item
SetGadgetText(#txt_Progress,"COPYING "+Str(Item)+" OF "+Str(Count)+" FILES")
EndIf
Until TaskFlag=0 ;Keeps repeating until TaskWindow() done
If Cancel=0 ;Skip when Cancel selected =1
SetGadgetText(#txt_Progress,"COPYING COMPLETE"):Beep(1500,200):Beep(1600,200):Beep(1700,200)
For Temp=1 To 1500:WaitWindowEvent(1):Next
Else
SetGadgetText(#txt_Progress,"COPYING CANCELLED")
For Temp=1 To 1500:WaitWindowEvent(1):Next
EndIf
CloseWindow(#win_Progress)
ProgressFlag=0 ;Progress thread is done flag
DisableGadget(#btn_Main,0)
EndIf
EndProcedure
Procedure TaskWindow(*Dummy)
For Item=1 To Count
If Cancel=1:Break:EndIf ;Cancel out of the task
Delay(1500) ;File Copy or whatever Task here
Next
TaskFlag=0 ;Task thread is done flag
DisableGadget(#btn_Main,0)
EndProcedure
OpenWindow(#win_Main,50,50,200,200,"Thread Play",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
ButtonGadget(#btn_Main,10,10,100,20,"Play Thread")
Repeat
If EventGadget()=#btn_Main
If ProgressFlag=0 And TaskFlag=0 ;Make sure both threads are done
Cancel=0:ProgressFlag=1:TaskFlag=1:Count=5
DisableGadget(#btn_Main,1)
ThreadTask=CreateThread(@TaskWindow(),0)
ThreadProg=CreateThread(@ProgressWindow(),0)
EndIf
EndIf
Until WaitWindowEvent()=#PB_Event_CloseWindow And ProgressFlag=0 And TaskFlag=0 ;Make sure both threads are done
End
PureBasic Help Documentation
Survival Guide by blueznl
PureBasic - A Beginners Guide by Kale