HMG and Multi-Threading

Discuss anything else that does not suite other forums.

Moderator: Rathinagiri

User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

HMG and Multi-Threading

Post by Roberto Lopez »

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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: HMG and Multi-Threading

Post by danielmaximiliano »

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
C:\harbour\tests\mt
Compile.bat

Code: Select all

c:\hb32\bin\hbmk2 -mt mttest11.prg
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
[attachment=0]2016-07-03 11_25_19-C__harbour_tests_mt_mttest11.exe.png[/attachment]
Attachments
2016-07-03 11_25_19-C__harbour_tests_mt_mttest11.exe.png
2016-07-03 11_25_19-C__harbour_tests_mt_mttest11.exe.png (3.74 KiB) Viewed 5646 times
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: HMG and Multi-Threading

Post by bpd2000 »

BPD
Convert Dream into Reality through HMG
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: HMG and Multi-Threading

Post by Roberto Lopez »

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
Image1.png (365.19 KiB) Viewed 5625 times
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)
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: HMG and Multi-Threading

Post by Roberto Lopez »

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.
Image2.png
Image2.png (281.7 KiB) Viewed 5619 times
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)
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: HMG and Multi-Threading

Post by andyglezl »

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
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: HMG and Multi-Threading

Post by Roberto Lopez »

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
Gracias! / Thanks!
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: HMG and Multi-Threading

Post by srvet_claudio »

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.
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: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.
Although I think it was not documented, I have implemented in HMG.3.4.3:
Doc -> External Guides and Tutorials -> Harbour for Beginners
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: HMG and Multi-Threading

Post by Roberto Lopez »

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.
Ok. Thanks!
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
Sorry... I have 3.4.2 here. I've missed the update.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
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

Post by Rathinagiri »

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.
Post Reply