HMG and Multi-Threading
Moderator: Rathinagiri
- Roberto Lopez
- HMG Founder
- Posts: 4004
- Joined: Wed Jul 30, 2008 6:43 pm
HMG and Multi-Threading
Hi All,
In one of my applications I have a complex server procedure that can take various minutes.
The problem with it, is that the user, thinks that the app 'hangs'. This is not really happening, but, the users hasn't enough patience
So, my idea to solve it, is to create a separate thread to call the remote procedure and let, at the same time, the user to keep in control and display an animation to show that the app is still running ok.
Has anyone some experience with multi-threading in HMG?
Some thing to consider?
Some problem to avoid?
TIA.
In one of my applications I have a complex server procedure that can take various minutes.
The problem with it, is that the user, thinks that the app 'hangs'. This is not really happening, but, the users hasn't enough patience
So, my idea to solve it, is to create a separate thread to call the remote procedure and let, at the same time, the user to keep in control and display an animation to show that the app is still running ok.
Has anyone some experience with multi-threading in HMG?
Some thing to consider?
Some problem to avoid?
TIA.
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
- danielmaximiliano
- Posts: 2612
- Joined: Fri Apr 09, 2010 4:53 pm
- Location: Argentina
- Contact:
Re: HMG and Multi-Threading
Hola Roberto :
muy buena pregunta... en VFP9 esmas sencillo trabajar con MultiHilos y que una funcion termine dependiendo que otra ya lo haya hecho...
en Harbour hay un lindo ejemplo que es este ya que su sencilles, este ejemplo se encuentra en
mttest11.prg
[attachment=0]2016-07-03 11_25_19-C__harbour_tests_mt_mttest11.exe.png[/attachment]
muy buena pregunta... en VFP9 esmas sencillo trabajar con MultiHilos y que una funcion termine dependiendo que otra ya lo haya hecho...
en Harbour hay un lindo ejemplo que es este ya que su sencilles, este ejemplo se encuentra en
Compile.batC:\harbour\tests\mt
Code: Select all
c:\hb32\bin\hbmk2 -mt mttest11.prg
Code: Select all
/*
* demonstration/test code for asynchronous screen updating without
* breaking foreground screen operations.
*
* Copyright 2008 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
*
*/
proc main()
local getList := {}
local cVar := space( 20 )
CLS
if ! hb_mtvm()
? "No MT support in HVM. Clock will not be shown."
WAIT
else
hb_threadStart( @thFunc() )
endif
@ 10, 10 SAY "Insert cVar:" GET cVar
READ
SetPos( 12, 0 )
? "Result -> [" + cVar + "]"
WAIT
return
func thFunc()
local cTime
while .T.
cTime := dtoc( date() ) + " " + time()
/* use hb_dispOutAt() which does not change current default
* color and cursor position so can be executed without bad
* side effects for other threads which updates screen.
* This functions also accepts colors as numeric values.
* Similar functionality have hb_dispBox() and hb_scroll().
* All these functions changes only screen buffer but do not
* touch cursor position and current color settings.
*/
hb_dispOutAt( 0, maxcol() - len( cTime ) + 1, cTime, "GR+/N" )
hb_idleSleep( 1 )
enddo
return nil
- Attachments
-
- 2016-07-03 11_25_19-C__harbour_tests_mt_mttest11.exe.png (3.74 KiB) Viewed 5648 times
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*
Saludos / Regards
DaNiElMaXiMiLiAnO
Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*
Saludos / Regards
DaNiElMaXiMiLiAnO
Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Re: HMG and Multi-Threading
BPD
Convert Dream into Reality through HMG
Convert Dream into Reality through HMG
- Roberto Lopez
- HMG Founder
- Posts: 4004
- Joined: Wed Jul 30, 2008 6:43 pm
Re: HMG and Multi-Threading
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:
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:
And This is the 'Show_Time' function:
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:
And This is the 'Show_Progress' function:
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...
This is how it will look like (please, believe me, the clock and progressbar stills updating )
Please take a look at this:
http://kresin.ru/en/hrbfaq_3.html#Doc11
The test project is attached.
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
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
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
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
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
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
Please take a look at this:
http://kresin.ru/en/hrbfaq_3.html#Doc11
The test project is attached.
- Attachments
-
- hmg.7z
- (1.63 KiB) Downloaded 308 times
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
- Roberto Lopez
- HMG Founder
- Posts: 4004
- Joined: Wed Jul 30, 2008 6:43 pm
Re: HMG and Multi-Threading
Claudio:
Two suggestions:
1. Maybe you could use multi-threading to call build procedure on IDE to avoid the 'app is not responding' dialog (and 'grayed' windows) when building large apps and one clicks on some of IDE windows. Additionally updates about build progress could be shown.
2. Could be useful to add a link to 'Harbour For Beginners' by Alexander Kresin to the HMG docs: http://kresin.ru/en/hrbfaq.html Since with the official 5.3 guide plus this one you have almost all you need to know.
Like someone said: Just my two cents
Two suggestions:
1. Maybe you could use multi-threading to call build procedure on IDE to avoid the 'app is not responding' dialog (and 'grayed' windows) when building large apps and one clicks on some of IDE windows. Additionally updates about build progress could be shown.
2. Could be useful to add a link to 'Harbour For Beginners' by Alexander Kresin to the HMG docs: http://kresin.ru/en/hrbfaq.html Since with the official 5.3 guide plus this one you have almost all you need to know.
Like someone said: Just my two cents
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
Re: HMG and Multi-Threading
Hola Roberto
Quizá este ejemplo de ideas...
en la opción 7, tienes acceso al menu principal y al menu de la opion ejectada.
viewtopic.php?f=6&t=4468&p=42585&hilit= ... enu#p42585
Quizá este ejemplo de ideas...
en la opción 7, tienes acceso al menu principal y al menu de la opion ejectada.
viewtopic.php?f=6&t=4468&p=42585&hilit= ... enu#p42585
Andrés González López
Desde Guadalajara, Jalisco. México.
Desde Guadalajara, Jalisco. México.
- Roberto Lopez
- HMG Founder
- Posts: 4004
- Joined: Wed Jul 30, 2008 6:43 pm
Re: HMG and Multi-Threading
Gracias! / Thanks!andyglezl wrote:Hola Roberto
Quizá este ejemplo de ideas...
en la opción 7, tienes acceso al menu principal y al menu de la opion ejectada.
viewtopic.php?f=6&t=4468&p=42585&hilit= ... enu#p42585
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
- srvet_claudio
- Posts: 2193
- Joined: Thu Feb 25, 2010 8:43 pm
- Location: Uruguay
- Contact:
Re: HMG and Multi-Threading
Very good idea Roberto, I'll have to study how the multithread works in Harbour, because I have only a superficial knowledge of the subject at C level.Roberto Lopez wrote:1. Maybe you could use multi-threading to call build procedure on IDE to avoid the 'app is not responding' dialog (and 'grayed' windows) when building large apps and one clicks on some of IDE windows. Additionally updates about build progress could be shown.
Although I think it was not documented, I have implemented in HMG.3.4.3:Roberto Lopez wrote:2. Could be useful to add a link to 'Harbour For Beginners' by Alexander Kresin to the HMG docs: http://kresin.ru/en/hrbfaq.html Since with the official 5.3 guide plus this one you have almost all you need to know.
Doc -> External Guides and Tutorials -> Harbour for Beginners
- Roberto Lopez
- HMG Founder
- Posts: 4004
- Joined: Wed Jul 30, 2008 6:43 pm
Re: HMG and Multi-Threading
Ok. Thanks!srvet_claudio wrote:Very good idea Roberto, I'll have to study how the multithread works in Harbour, because I have only a superficial knowledge of the subject at C level.
Sorry... I have 3.4.2 here. I've missed the update.srvet_claudio wrote: Although I think it was not documented, I have implemented in HMG.3.4.3:
Doc -> External Guides and Tutorials -> Harbour for Beginners
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
- Rathinagiri
- Posts: 5471
- Joined: Tue Jul 29, 2008 6:30 pm
- DBs Used: MariaDB, SQLite, SQLCipher and MySQL
- Location: Sivakasi, India
- Contact:
Re: HMG and Multi-Threading
Wonderful Roberto! It is a real break through!
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
South or North HMG is worth.
...the possibilities are endless.