Ver Mensaje Individual
  #3  
Antiguo 05-02-2015
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.197
Reputación: 20
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
Como te indica Neftali, es posible pero siempre que vayas a trabajar con un elemento de la VCL desde un hilo, sebes usar si o si, Synchronize.

Te pongo un ejemplo simple de un hilo que modifica un Label en un Form modal:

Código:
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include "Unit3.h"
//---------------------------------------------------------------------------
class ThPrueba : public TThread
{
private:
  TForm3 *Form;
  String LS;
protected:
        void __fastcall Execute();
public:
        __fastcall ThPrueba(TForm3 *form, bool CreateSuspended);
        void __fastcall ActualizaLabel();
};
//---------------------------------------------------------------------------
#endif
Código:
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"

#pragma package(smart_init)

__fastcall ThPrueba::ThPrueba(TForm3 *form, bool CreateSuspended)
        : TThread(CreateSuspended)
{
  Form = form;
}

void __fastcall ThPrueba::ActualizaLabel()
{
  Form->Label1->Caption = LS;
}


//---------------------------------------------------------------------------
void __fastcall ThPrueba::Execute()
{
   int n=0;
   while(!Terminated){
     LS = n++;
     Synchronize(ActualizaLabel);
     Sleep(500);
   }
}

Saludos.

Última edición por escafandra fecha: 09-03-2020 a las 08:00:24.
Responder Con Cita