Unable to terminate thread

Moderator: Rathinagiri

Post Reply
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Unable to terminate thread

Post 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



huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: Unable to terminate thread

Post by huiyi_ch »

hello friends
I can't terminate the child thread in the main thread,is my code any questions?
User avatar
AUGE_OHR
Posts: 2060
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Unable to terminate thread

Post 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
have fun
Jimmy
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: Unable to terminate thread

Post by huiyi_ch »

hello friends,
Why can a thread created with hb_threadStart() not terminate with hb_threadQuitRequest() or HB_threadTerminateAll()?
User avatar
SALINETAS24
Posts: 667
Joined: Tue Feb 27, 2018 3:06 am
DBs Used: DBF
Contact:

Re: Unable to terminate thread

Post 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
Como dijo el gran pensador Hommer Simpson..., - En este mundo solo hay 3 tipos de personas, los que saben contar y los que no. :shock:
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: Unable to terminate thread

Post 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?
User avatar
AUGE_OHR
Posts: 2060
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: Unable to terminate thread

Post 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.
have fun
Jimmy
User avatar
apais
Posts: 440
Joined: Fri Aug 01, 2008 6:03 pm
DBs Used: DBF
Location: uruguay
Contact:

Re: Unable to terminate thread

Post by apais »

GUI elements must be on the same thread.
Angel Pais
Web Apps consultant/architect/developer.
Post Reply