Thanks!
Since is a rainy and cold Sunday here, I've tried the thing myself prior to read your answers
The main problem is that the Harbour basic demos does not works for HMG because by default, public variables are not visible between threads, except you specify that, so, the trick is there...
I've created a simple app that updates a clock and a progressbar (each one on its own thread) while the main thread still working (responding to user events) normally.
So:
Code: Select all
#include <hmg.ch>
Function Main
Load Window Main
Main.Center
Main.Activate
Return
All we understand that
I've added one button to it (button_1) to start a thread that will show a clock. This is the action procedure:
Code: Select all
#include "hmg.ch"
#define HB_THREAD_INHERIT_PUBLIC 1
declare window Main
Function main_button_1_action
IF !hb_mtvm()
MSGSTOP("There is no support for multi-threading.")
ELSE
hb_threadStart( HB_THREAD_INHERIT_PUBLIC , @Show_Time() )
ENDIF
Return Nil
And This is the 'Show_Time' function:
Code: Select all
FUNCTION Show_Time()
LOCAL cTime
* disable 'Start Thread' button to avoid accidentally start a new thread!
main.button_1.enabled := .f.
* please note that this function will NEVER return the control!
* but do not 'locks' the user interface since it is running in a separate thread
DO WHILE .T.
main.label_1.value := Time()
hb_idleSleep( 1 )
ENDDO
RETURN nil
I've added another button to the main window (button_2) to start a thread that will show a progressbar updating (forever). This is the action procedure:
Code: Select all
#include "hmg.ch"
#define HB_THREAD_INHERIT_PUBLIC 1
declare window Main
Function main_button_2_action
IF !hb_mtvm()
MSGSTOP("There is no support for multi-threading, ProgressBar will not be seen.")
ELSE
hb_threadStart( HB_THREAD_INHERIT_PUBLIC , @Show_Progress() )
ENDIF
Return Nil
And This is the 'Show_Progress' function:
Code: Select all
FUNCTION Show_Progress()
* disable 'Start Progress' button to avoid accidentally start a new thread!
main.button_2.enabled := .f.
DO WHILE .T.
nValue := main.progressBar_1.value
nValue ++
if nValue > 10
nValue := 1
endif
main.progressBar_1.value := nValue
hb_idleSleep( .5 )
ENDDO
RETURN nil
We have now two thread (besides the main) running simultaneously.
To test the concept, another button (button_3) will show a MsgInfo (that will stop the main thread) while the clock and the progressbar stills updating...
Code: Select all
#include "hmg.ch"
declare window Main
Function main_button_3_action
MSGINFO('Clock and ProgressBar keep updating even the main thread is stopped at this MsgInfo!!!')
Return Nil
This is how it will look like (please, believe me, the clock and progressbar stills updating

)

- Image1.png (365.19 KiB) Viewed 5939 times
Please take a look at this:
http://kresin.ru/en/hrbfaq_3.html#Doc11
The test project is attached.