Jump to content
Fivewin Brasil

StartThread() - Start a progress bar windows


kapiaba

Recommended Posts

Galera como eu faço isso no xHARBOUR?



/*
StartThread() // Start a progress bar windows
aFiles := DirectoryRecurse(PathPictArt + "*.jpg")
StopThread() // Stop and close this windows
*/

PROCEDURE Main

LOCAL oThread := Thread():new() // create thread object

CLS

oThread:start( "Sum", 10000 ) // sum numbers from 1

// to 10000
DO WHILE .T. // display characters during

?? "." // the calculation

Sleep(10)

IF ! oThread:active // Check if thread still runs
EXIT
ENDIF

ENDDO

? "The sum is:", oThread:result

RETURN

FUNCTION Sum( nNumber )

LOCAL i, nSum := 0

FOR i:=1 TO nNumber

nSum += i

IF i % 100 == 0 // progress display
DispOutAt( MaxRow(), 0, i )
ENDIF

NEXT

RETURN nSum


Link to comment
Share on other sites

Obg. mas esse exemplo, nem passa na compilação.



Lines 220, Functions/Procedures 4, pCodes 693
Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
inetto.c:
Turbo Incremental Link 5.69 Copyright (c) 1997-2005 Borland
Error: Unresolved external '_HB_FUN_DESTROYMUTEX' referenced from C:\FWH1306\SAMPLES\INETTO.OBJ
Error: Unresolved external '_HB_FUN_THREADGETCURRENT' referenced from C:\FWH1306\SAMPLES\INETTO.OBJ
Error: Unresolved external '_HB_FUN_SUBSCRIBE' referenced from C:\FWH1306\SAMPLES\INETTO.OBJ
Error: Unresolved external '_HB_FUN_NOTIFY' referenced from C:\FWH1306\SAMPLES\INETTO.OBJ
* Linking errors *
Link to comment
Share on other sites

  • 2 years later...

Olá,

Obrigado, o Culik me falou isto ontem, para nao usar thread.

ele me indicou background.

Acho que vai resolver o que eu preciso que é deixar uma rotina em paralelo por pouco tempo, ao entrar no sistema, para verificar se a copia do sistema é ok,

sem parar o processamento para o usuario.

Narlem

Link to comment
Share on other sites

concordo plenamente, e como harbour e xharbour são open source, se tivessem interesse, seria muito fácil trazer isto para xharbour.

Já não somos tão numerosos como gostaríamos, ainda se divide em 3, harbour, xharbour.org, xharbour.com...

mas projeto com código aberto é assim mesmo, alguns discordam e pegam o código e "começam um novo" projeto, enfraquecendo todos...

Quanto a sqlrdd para harbour, acho que num futuro próximo, é só um sonho ...

Narelm

Link to comment
Share on other sites

Bom dia galera.

Existe funções do Habour para criar 'background tasks'. Achei interessante!

Abs.

HB_IDLEADD()
Adds the background task.
Syntax
HB_IDLEADD( <bAction> ) --> nHandle
Argument(s)
<bAction> is a codeblock that will be executed during idle states. There are no arguments passed to this codeblock during evaluation.
Returns
<nHandle> The handle (an integer value) that identifies the task. This handle can be used for deleting the task.
Description
HB_IDLEADD() adds a passed codeblock to the list of background tasks that will be evaluated during the idle states. There is no limit for the number of tasks.
Example(s)

nTask := HB_IDLEADD( {|| SayTime() } )

Referência: http://www.fivetechsoft.com/harbour-docs/api.html#hb_idleadd

Link to comment
Share on other sites

  • 3 months later...

// Exemplo1 do xHarbour 

#include "hbclass.ch"

PROCEDURE Main()

  LOCAL nStart := Seconds()
  LOCAL oMyObject := MyClass()
  LOCAL MethodPtr := HB_ObjMsgPtr( oMyObject, "Count" )
  LOCAL xThread

  CLEAR SCREEN

  nStart := Seconds()

  // 1st param is the Startup Function, 2nd. is Self if 1st param is a Method or NIL otherwise,
  // rest are paramaters to be passed to the Function/Method.
  StartThread ( @MyThreadFunc(), 2, "1st Thread:",     0,  5000 )
  StartThread ( @MyThreadFunc(), 4, "2nd Thread:",  5000, 10000 )
  StartThread ( @MyThreadFunc(), 6, "3rd Thread:", 10000, 15000 )

  WaitForThreads()
  @ 8, 0 SAY "Threads Time:" + Str( Seconds() - nStart )

  nStart := Seconds()

  // StartThread() for methods can be called using an already available
  // Method Pointer or using a method name
  StartThread ( oMyObject, "Count", 10, "1st Thread:",     0,  5000 )
  StartThread ( oMyObject, "Count", 12, "2nd Thread:",  5000, 10000 )
  StartThread ( oMyObject, MethodPtr, 14, "3rd Thread:", 10000, 15000 )

  WaitForThreads()
  @ 16, 0 SAY "[METHODS] Threads Time:" + Str( Seconds() - nStart )

  nStart := Seconds()

  MyThreadFunc( 18, "1st Run:",     0,  5000 )
  MyThreadFunc( 20, "2nd Run:",  5000, 10000 )
  MyThreadFunc( 22, "3rd Run:", 10000, 15000 )

  @ 24, 0 SAY  "Sequential Time:" + Str( Seconds() - nStart )

  Inkey(0)
RETURN

PROCEDURE MyThreadFunc( nRow, cName, nStart, nMax )

  LOCAL i

  FOR i := nStart TO nMax
     //@ nRow, 10 SAY cName + Str( i )
     // Atomic operation
     DispOutAt(nRow, 10, cName + Str( i ))
  NEXT

RETURN

CLASS MyClass
    METHOD Count( nRow, cName, nStart, nMax )
ENDCLASS

METHOD Count( nRow, cName, nStart, nMax ) CLASS MyClass
  LOCAL i

  FOR i := nStart TO nMax
     //@ nRow, 10 SAY cName + Str( i )
     // Atomic operation
     DispOutAt(nRow, 10, cName + Str( i ))
  NEXT

RETURN NIL

 

 

 

// Exemplo 2

*
* Complex example of Multi thread usage
*
* Giancarlo Niccolai
* $Id: mtcomplex.prg 9279 2011-02-14 18:06:32Z druzus $
*
* Here we have a main thread counting, and some secondary
* threads counting too (in different fashons).
* A control thread is notified when each secondary
* thread finishes, and some of the secondary threads
* are killed before they are able to reach the end.
*

PROCEDURE Main()
   LOCAL i
   LOCAL Mutex := HB_MutexCreate()
   LOCAL Mutex2 := HB_MutexCreate()
   LOCAL Thread4Handle, MonitorHandle
   LOCAL bKill := .F.

   Mutex2 := NIL
   SET OUTPUT SAFETY ON

   set color to w+/b
   CLEAR SCREEN
   @1,15 SAY "X H A R B O U R - Complex multithreading test"
   @3,17 SAY "Press any key to terminate in every moment"

   StartThread ( @ThreadFunc(), 10, "1st. Thread", 100, Mutex )
   StartThread ( @ThreadFunc(), 11, "2nd. Thread", 250, Mutex )

   /* Test of the { codeblock } grammar */
   StartThread ( { | nRow, cName, nLoops, Mtx| ThreadFunc(nRow, cName, nLoops, Mtx) } ;
         , 12, "3rd. Thread", 300, Mutex )

   Thread4Handle := StartThread( @ThreadFunc(), 13, "4th. Thread", 700, Mutex )

   /* Notice the "function name" grammar */
   MonitorHandle := StartThread ( "MonitorFunc", Mutex )
   *MonitorHandle := StartThread ( @MonitorFunc() , Mutex )

   StartThread ( @FourthMonitor(), Thread4Handle, Mutex )

   FOR i := 0 TO 500
      @ 14, 10 SAY 'Main Thread:' + Str( i, 4 )

      IF i == 100
         StopThread( Thread4Handle )
         @ 14, 27 SAY "(Killed 4th. Thread!)" 
      ENDIF

      HB_GCAll( .T. )
      ThreadSleep( 30 )
      IF Inkey() != 0
         bKill := .T.
         EXIT
      ENDIF
   NEXT

   IF bKill
      @17, 10 SAY 'Killing all threads on user requests      '
      KillAllThreads()
   ELSE
      @ 17, 10 SAY 'Cycle over, stopping Monitor     '
      KillThread( MonitorHandle )
      @ 17, 10 SAY 'Cycle over, Monitor Stopped     '
   ENDIF
      
   @18, 10 SAY 'Waiting for thread termination                  '      
   WaitforThreads()

   @ 19, 10 SAY 'Program over - press a key    '
   Inkey( 0 )

   @ 24, 00

PROCEDURE ThreadFunc( nRow, cName, nMax, Mutex )

   LOCAL i

   FOR i := 0 TO nMax
      @ nRow, 10 SAY cName + Str( i, 4 )
      WaitForThis( nRow ) // calling a proc inside a thread
   NEXT

   Notify( Mutex, cName )
   @ nRow, 10 SAY cName + ': DONE '+ space(20)
RETURN

PROCEDURE WaitForThis( nRow )

   ThreadSleep( 30  )

RETURN

PROCEDURE MonitorFunc( Mutex )

   LOCAL cName

   @ 5, 5 SAY "Entering Monitor Thread"
   @ 6, 5 SAY "->> Thread finished:"

   DO WHILE .T.
      cName := Subscribe( Mutex )

      IF cName != NIL
         @ 6, 26 SAY cName
      ELSE
         @ 6, 26 SAY "Unknown ..             " 
      ENDIF

      ThreadSleep( 1500 )
      @ 6, 26 SAY "                          "
   ENDDO

RETURN

PROCEDURE FourthMonitor( ThreadHandle, Mutex )

   @ 8, 5 SAY "Waiting for 4th. Thread to finish..."

   JoinThread( ThreadHandle )

   @ 8, 5 SAY "FourthMonitor: 4th. Thread Finished/Killed!"

RETURN
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...