Page 1 of 1

Unable to terminate thread

Posted: Sat Jan 11, 2020 3:53 pm
by huiyi_ch
The following example allows you to create multiple threads, but you cannot terminate them all at once.Please help to solve this problem.

Code: Select all


#include <hmg.ch>
Function Main()
private pThID:={}
   IF !hb_mtvm()
      MSGSTOP("There is no support for multi-threading")
      QUIT
   ENDIF

   DEFINE WINDOW Form_1 AT 100 , 100 WIDTH 550 HEIGHT 380 MAIN
      @  50, 100 BUTTON Button_1 CAPTION "Notifier MT"  ACTION CreateThread()
      @ 100, 100 BUTTON Button_2 CAPTION "Terminate Thread"  ACTION CloseThread()

   END WINDOW
   Form_1.Activate

Return Nil


PROCEDURE CreateThread
local i:=0
 for i:=1 to 5
   aadd(pThID,hb_threadStart( @Notifier()))
 next 
RETURN

Function Notifier( )
LOCAL nHeight, nWidth
LOCAL nLeft, nTop
LOCAL hWnd, aBcolor, nWinRow, nWinCol
LOCAL nTimeIni
LOCAL hWinMain

   nHeight := 82
   nWidth := 604
   nLeft := (GetDesktopWidth() - 5) - nWidth
   nTop := (GetDesktopHeight() - 50) - nHeight
   nWinRow := nTop
   nWinCol := nLeft

   nLeft := hb_Random() * ( GetDesktopWidth()  - nWidth  - 10 )
   nTop  := hb_Random() * ( GetDesktopHeight() - nHeight - 10 )

 
   DEFINE WINDOW Win_Msg AT nTop, nLeft WIDTH nWidth HEIGHT nHeight ; // TOPMOST
      NOMAXIMIZE NOSIZE 
	  
	  DEFINE BUTTON Button_1
        ROW    13
        COL    200
        WIDTH  160
        HEIGHT 28
        ACTION ( msginfo("OK") )
        CAPTION "CLICKED"
     END BUTTON

   END WINDOW
   Win_Msg.Activate
Return Nil

STATIC FUNCTION CloseThread()
LOCAL i
   FOR i := 1 TO LEN( pThID )
      IF pThID[ i ] <> NIL
         hb_threadDetach( pThID[ i ] )   // close thread handle
         hb_threadQuitRequest( pThID[ i ] )   // terminate thread
         pThID[ i ] := NIL
      ENDIF
   NEXT
RETURN NIL




Re: Unable to terminate thread

Posted: Sun Jan 12, 2020 2:33 am
by huiyi_ch
hello friends
I can't terminate the child thread in the main thread,is my code any questions?

Re: Unable to terminate thread

Posted: Sun Jan 12, 2020 6:01 am
by AUGE_OHR
hi,

as i can say you have to .RELEASE when use .ACTIVATE so you need WINDOW "Name" in your Array.
if you want to use same WINDOW multi time you have pass "Name" and use

Code: Select all

   DEFINE WINDOWS &Macro

Re: Unable to terminate thread

Posted: Mon Jan 13, 2020 2:06 pm
by huiyi_ch
hello friends,
Why can a thread created with hb_threadStart() not terminate with hb_threadQuitRequest() or HB_threadTerminateAll()?

Re: Unable to terminate thread

Posted: Mon Jan 13, 2020 6:33 pm
by SALINETAS24
Hola, creo que el problema lo tienes en la ventana, desde mi punto de vista hb_theadStart() es una función para realizar procesos invisibles (reordenación de tablas, indices, impresión documentos, etc,) y así poder forzar su salida con hb_threadQuitRequest () o HB_threadTerminateAll (). Cuando abres una ventana la única forma que tendrás para abandonar la función será cerrandola ya que esa ventana se abre en un nuevo hilo y en principio (salvo que alguien diga lo contrario), desde el PRG principal no tienes control sobre la nueva ventana.
Sl2

Re: Unable to terminate thread

Posted: Tue Jan 14, 2020 1:35 pm
by huiyi_ch
Thank you very much for your response.The question now is: is there a feasible way to terminate a child thread from the main thread?

Re: Unable to terminate thread

Posted: Thu Jan 16, 2020 12:29 am
by AUGE_OHR
hi

you use the Code from c:\hmg.3.4.4\SAMPLES\MultiThread\MT_ClassicDemo\MT_ClassicDemo.prg

but those Threads do not call a new "DEFINE Window" :!:

so you have to close Window with .Release and than Thread will "stop".
not sure if need to "clean up" with ThreadDetach() to "free" Handle.

Re: Unable to terminate thread

Posted: Thu Jan 16, 2020 6:34 pm
by apais
GUI elements must be on the same thread.