Alaska Software Inc. - Thread Question
Username: Password:
AuthorTopic: Thread Question
Carlos A Beling Thread Question
on Wed, 04 Sep 2019 09:46:09 -0300
Good day.
I want to close all the threads at the end of the application (even that 
are waiting some answer) even it has one kind of thread (printing) that 
I want to wait until it finishes.
Please, how do I can do it?

Fraternally
Beling
Boris BorzicRe: Thread Question
on Wed, 04 Sep 2019 19:05:19 +0200
Carlos A Beling <beling@bipbip.com.br> wrote in
news:75902c5e$79d9e27f$3f94@news.alaska-software.com: 

> I want to close all the threads at the end of the application (even
> that are waiting some answer) even it has one kind of thread
> (printing) that I want to wait until it finishes.
> Please, how do I can do it?

Create a global function used to signal a shutdown event:

function Shutdown( lSet )
   static lShutdown := .f.
   if lSet == .t.
      lShutdown := .t.
   endif
   Return lShutdown

----------------
if app is running as a windows service, you can do this:

oService := ServiceApp()
oService:main := {||StartMyService()}  your background threads
oService:stop := {||Shutdown(.T.)}  signal all threads to terminate
oService:start()

----------------
if running as a windows GUI, add to main thread:

while ! Shutdown()
   if (nEvent := AppEvent(@mp1, @mp2, @oXbp, 10)) == xbe_None
   
   else
      oXbp:handleEvent(nEvent, mp1, mp2)
      if nEvent == xbeK_ESC .and. ConfirmBox(nil, "Terminate?", "Quit",;
     	    	XBPMB_OKCANCEL, XBPMB_WARNING+XBPMB_MOVEABLE+XBPMB_APPMODAL,;
            XBPMB_DEFBUTTON2) == XBPMB_RET_OK

    	   Shutdown(.T.)
    	endif
   endif
end


----------------
In each thread's main loop check if a shutdown is in progress, eg:

procedure PrintingThread()
   while !shutdown()
       check print queue...
    	 print document, etc...
    	sleep(100)
   end
   Return 


Best regards,
Boris Borzic

http://xb2.net
http://sqlexpress.net
industrial strength Xbase++ development tools
Carlos A Beling Re: Thread Question
on Wed, 04 Sep 2019 17:30:18 -0300
Hello Boris:
Good day.
Many thanks again.

Fraternally
Beling

Em 04/09/2019 14:05, Boris Borzic escreveu:
> Carlos A Beling <beling@bipbip.com.br> wrote in
> news:75902c5e$79d9e27f$3f94@news.alaska-software.com:
> 
>> I want to close all the threads at the end of the application (even
>> that are waiting some answer) even it has one kind of thread
>> (printing) that I want to wait until it finishes.
>> Please, how do I can do it?
> 
> Create a global function used to signal a shutdown event:
> 
> function Shutdown( lSet )
>     static lShutdown := .f.
>     if lSet == .t.
>        lShutdown := .t.
>     endif
>     Return lShutdown
> 
> ----------------
> if app is running as a windows service, you can do this:
> 
> oService := ServiceApp()
> oService:main := {||StartMyService()}  your background threads
> oService:stop := {||Shutdown(.T.)}  signal all threads to terminate
> oService:start()
> 
> ----------------
> if running as a windows GUI, add to main thread:
> 
> while ! Shutdown()
>     if (nEvent := AppEvent(@mp1, @mp2, @oXbp, 10)) == xbe_None
>     
>     else
>        oXbp:handleEvent(nEvent, mp1, mp2)
>        if nEvent == xbeK_ESC .and. ConfirmBox(nil, "Terminate?", "Quit",;
>       	    	XBPMB_OKCANCEL, XBPMB_WARNING+XBPMB_MOVEABLE+XBPMB_APPMODAL,;
>              XBPMB_DEFBUTTON2) == XBPMB_RET_OK
> 
>      	   Shutdown(.T.)
>      	endif
>     endif
> end
> 
> 
> ----------------
> In each thread's main loop check if a shutdown is in progress, eg:
> 
> procedure PrintingThread()
>     while !shutdown()
>         check print queue...
>      	 print document, etc...
>      	sleep(100)
>     end
>     Return
> 
>
Carlos A Beling Re: Thread Question
on Thu, 05 Sep 2019 09:35:26 -0300
Hello Boris.
Good day.
Many thanks again.

Fraternally
Beling

Em 04/09/2019 14:05, Boris Borzic escreveu:
> Carlos A Beling <beling@bipbip.com.br> wrote in
> news:75902c5e$79d9e27f$3f94@news.alaska-software.com:
> 
>> I want to close all the threads at the end of the application (even
>> that are waiting some answer) even it has one kind of thread
>> (printing) that I want to wait until it finishes.
>> Please, how do I can do it?
> 
> Create a global function used to signal a shutdown event:
> 
> function Shutdown( lSet )
>     static lShutdown := .f.
>     if lSet == .t.
>        lShutdown := .t.
>     endif
>     Return lShutdown
> 
> ----------------
> if app is running as a windows service, you can do this:
> 
> oService := ServiceApp()
> oService:main := {||StartMyService()}  your background threads
> oService:stop := {||Shutdown(.T.)}  signal all threads to terminate
> oService:start()
> 
> ----------------
> if running as a windows GUI, add to main thread:
> 
> while ! Shutdown()
>     if (nEvent := AppEvent(@mp1, @mp2, @oXbp, 10)) == xbe_None
>     
>     else
>        oXbp:handleEvent(nEvent, mp1, mp2)
>        if nEvent == xbeK_ESC .and. ConfirmBox(nil, "Terminate?", "Quit",;
>       	    	XBPMB_OKCANCEL, XBPMB_WARNING+XBPMB_MOVEABLE+XBPMB_APPMODAL,;
>              XBPMB_DEFBUTTON2) == XBPMB_RET_OK
> 
>      	   Shutdown(.T.)
>      	endif
>     endif
> end
> 
> 
> ----------------
> In each thread's main loop check if a shutdown is in progress, eg:
> 
> procedure PrintingThread()
>     while !shutdown()
>         check print queue...
>      	 print document, etc...
>      	sleep(100)
>     end
>     Return
> 
>
Jack DuijfRe: Thread Question
on Sat, 14 Sep 2019 16:48:19 +0200
On Wed, 04 Sep 2019 09:46:09 -0300, Carlos A Beling <beling@bipbip.com.br> wrote:

>Good day.
>I want to close all the threads at the end of the application (even that 
>are waiting some answer) even it has one kind of thread (printing) that 
>I want to wait until it finishes.
>Please, how do I can do it?
>
>Fraternally
>Beling

Try this:

Subclass Thread, see Xbase++ help on "Thread object" 

aInfo := ThreadInfo( THREADINFO_TOBJ)

For ni := 1 To Len(aInfo)
   oThread := aInfo[ni]
   If oThread <> ThreadObject() .and. oThread:Active
      If IsMethod(oThread,"Terminate")
          oThread:Terminate()
          Do While oThread:Active 
                Sleep(1)
          Enddo
      Endif 	
   Endif 
Next  ni

Regards
Jack Duijf

-------------------------------------------------------------
Also a member off the XXP (http://www.xxp.nl)
Carlos A Beling Re: Thread Question
on Mon, 16 Sep 2019 15:32:43 -0300
Hello Jack:
good day.
Many thanks.

Fraternally
Beling


Em 14/09/2019 11:48, Jack Duijf escreveu:
> On Wed, 04 Sep 2019 09:46:09 -0300, Carlos A Beling <beling@bipbip.com.br> wrote:
> 
>> Good day.
>> I want to close all the threads at the end of the application (even that
>> are waiting some answer) even it has one kind of thread (printing) that
>> I want to wait until it finishes.
>> Please, how do I can do it?
>>
>> Fraternally
>> Beling
> 
> Try this:
> 
> Subclass Thread, see Xbase++ help on "Thread object"
> 
> aInfo := ThreadInfo( THREADINFO_TOBJ)
> 
> For ni := 1 To Len(aInfo)
>     oThread := aInfo[ni]
>     If oThread <> ThreadObject() .and. oThread:Active
>        If IsMethod(oThread,"Terminate")
>            oThread:Terminate()
>            Do While oThread:Active
>                  Sleep(1)
>            Enddo
>        Endif 	
>     Endif
> Next  ni
> 
> Regards
> Jack Duijf
> 
> -------------------------------------------------------------
> Also a member off the XXP (http://www.xxp.nl)
>