Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Consulta de MultiHilos ( thread ) (https://www.clubdelphi.com/foros/showthread.php?t=42784)

jmlifi 23-04-2007 16:23:15

Consulta de MultiHilos ( thread )
 
Quiero que se repita (infinitamente) en mi aplicación un Thread con un intervalo 20 segundos. Sólo se repite la primera vez.

Codigo:
Código Delphi [-]
unit Gestor1;
interface
uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Menus, ExtCtrls, Gauges, DB, StdCtrls,
  Buttons, ToolWin, ComCtrls, DBCtrls, Registry, AuxFun, ImgList,FileCtrl,
  FMTBcd, SqlExpr, DBClient, Windows, SqlTimSt, xmldom, Provider, Xmlxform,
  ExportDS, SME2Cell, SME2XML, XMLIntf, msxmldom, XMLDoc, ShellAPI, DBXpress,
  ActiveX, Variants;
type

   TThread6 = class (TThread)
  private
      OwnerHandle: HWND;
      procedure RecepAutoVeriOferta;
  protected
      procedure Execute; override;
  published
      constructor Create(Owner: TForm);
     // destructor Destroy; override;
  end;
  TPrincipal = class(TForm)
    MainMenu1: TMainMenu;
    Clientes1: TMenuItem;
    Clientes: TMenuItem;
    Piezas: TMenuItem;
    ..
    ..
 

procedure TPrincipal.Ejecuta;
var
   i, j, n           : integer;
   Cod, aux, Accesos : String;
   fRec : TSearchRec;
begin
   LeeDatosEntorno;
   Thread6 := TThread6.Create(self);
    ..
    ..    
end;

Constructor TThread6.Create(Owner: TForm);
begin
  inherited Create(false);
  OwnerHandle := Owner.Handle;
  Priority := tpNormal;
  FreeOnTerminate := false;
  suspended := false;
end;
 
 
destructor TThread6.Destroy;
begin
  inherited Destroy;
end;
 
procedure TThread6.Execute;
begin
repeat
  sleep(20000);
  CoInitialize(nil);
  RecepAutoVeriOferta;
  CoUninitialize;
until terminated;
end;


Quitando el procedure destroy también me repite una vez. ¿a qué se debe? me estoy volviendo loco.
saludos

droguerman 23-04-2007 17:20:05

has intentado poner un bucle infinito dentro de execute??
while true do
begin
...
end;

jmlifi 23-04-2007 17:34:54

Cita:

Empezado por droguerman
has intentado poner un bucle infinito dentro de execute??
while true do
begin
...
end;

asi?

Código Delphi [-]
procedure TThread6.Execute;
begin
while True do begin
  sleep(20000);
  CoInitialize(nil);
  RecepAutoVeriOferta;
  CoUninitialize;
end;
end;

sigue sin repertirse. Sólo se ejecuta una vez

droguerman 23-04-2007 18:55:53

intenta esto:
Código Delphi [-]

THebra = class(TThread)
  public
    procedure RecepAutoVeriOferta;
    procedure execute; override;
end;


procedure THebra.execute;
begin
  while true do  
  begin
     synchronize(RecepAutoVeriOferta);
     sleep(20000);
  end;
end;
dentro de recepAutoVeriOferta puedes llamar a Coinitialize o tambien en el constructor

jmlifi 24-04-2007 09:38:48

He modificado el código de la siguiente manera:
Código Delphi [-]
  
TThread6 = class (TThread)
  public
      constructor Create;
      procedure RecepAutoVeriOferta;
      procedure Execute; override;
  end;
 
Constructor TThread6.Create;
begin
  inherited Create(true);
  Priority := tpNormal;
  FreeOnTerminate := false;
end;
 
procedure TThread6.Execute;
begin
while true do begin
  sleep(22000);
  SynChronize(RecepAutoVeriOferta)
end;
end;

Sigue sin funcionar. sólo ejecuta la primera vez.
En el Event Log del Borland Developer Studio 2006 aparece lo siguiente:
Thread Exit: Thread ID: 2580. Process GestorCS.exe (3924)

seoane 24-04-2007 13:38:29

Comprueba que dentro del bucle no se este produciendo alguna excepcion. Piensa que hay excepciones silenciosas (abort), que pueden pasar desapercibidas.

Prueba a meter el contenido del bucle dentro de un bloque try ... except
Código Delphi [-]
procedure TThread6.Execute;
begin
  CoInitialize(nil);
  repeat
    try
      sleep(20000);    
      RecepAutoVeriOferta;    
    except
        // Aqui trata el error
    end;  
  until terminated;
  CoUninitialize;
end;


La franja horaria es GMT +2. Ahora son las 06:04:42.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi