Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   pregunta hace rato no programo en delphi? (https://www.clubdelphi.com/foros/showthread.php?t=69221)

julyus 03-08-2010 01:19:29

pregunta hace rato no programo en delphi?
 
tengo una preguntota de 100000 dollares se pueden crear dos clases en un mismo form o datamodulo ?? gracias por la respuesta:confused:

ecfisa 03-08-2010 01:59:55

Hola Julyus.

Si.

Siempre que las clases no tengan el mismo nombre. si no te va dar error de identificador redeclarado.

Es decir que podés hacer:
Código:

type
  Clase1 = class
  end;
  Clase2 = class
  end;
  ClaseN = class
  end;

Pero no:
Código:

type
    Clase1 = class
    end;
    Clase1 = class  // identifier redeclarer 'clase1'
    end;


Saludos.

ecfisa 03-08-2010 04:30:09

Casi me olvido !!!!


Donde me depositas los u$s 100000 ??? :)


Saludos.

Neftali [Germán.Estévez] 03-08-2010 10:53:37

Basta con que te des una vuelta por el directorio de la VCL (normalmente en: "c:\Archivos de programa\Borland\Delphi6\Source\Vcl\" o similar) y mires alguno de los ficheros.

StdCtrls.pas, por ejemplo, no sólo tiene más de 1 clase, sino que tiene todas (o casi todas) las clases de los componentes de la paleta Standard de Delphi.

julyus 03-08-2010 17:00:04

este el el codigo a ver que handa mal aca
 
hola el problema se me presenta en las clases TFixedConnectionPool y TCleanupThread si ven una necesita de la otra y si las cambio de posicion pasa lo contrario es la otra la que no la reconoce ose ano me la deja declarar por que ????

gracias por la ayuda :confused:

Código Delphi [-]
unit Unit1;

interface

uses
  SysUtils, Classes, ADODB, syncobjs, Windows , DB, Dialogs;

type
  IConnection = Interface(IInterface)
  function Connection: TADOConnection;
  function GetRefCount: Integer;
  function GetLastAccess: TDateTime;
  property LastAccess: TDateTime read GetLastAccess;
  property RefCount: Integer read GetRefCount;
end;


type
  TDataModule1 = class(TDataModule, IConnection)
  private
    { Private declarations }
    protected
  FRefCount: Integer;
  FLastAccess: TDateTime;
  CriticalSection: TCriticalSection;
  Semaphore: THandle;
  function _AddRef: Integer; stdcall;
  function _Release: Integer; stdcall;
  {IConnection methods}
  function GetLastAccess: TDateTime;
  function GetRefCount: Integer;
  public
  { Public declarations }
    function Connection: TADOConnection;
  end;


  TFixedConnectionPool = class(TObject)
private
  FPool: array of IConnection;
  FPoolSize: Integer;
  FTimeout: LargeInt;
  CleanupThread: TCleanupThread;
  Semaphore: THandle;
  CriticalSection: TCriticalSection;
public
  constructor Create(const PoolSize: Integer = 10;
    const CleanupDelayMinutes: Integer = 5;
    const Timeoutms: LargeInt = 10000); overload;
  destructor Destroy; override;

  function GetConnection: IConnection;
end;

type
  TCleanupThread = class(TThread)
  private
  FCleanupDelay: Integer;
  protected

  CriticalSection: TCriticalSection;
  FixedConnectionPool: TFixedConnectionPool;
  procedure Execute; override;
  constructor Create(CreateSuspended: Boolean;
    const CleanupDelayMinutes: Integer);
end;

var
  DataModule1: TDataModule1;

implementation

{$R *.dfm}


function TDataModule1._AddRef: Integer;
begin

CriticalSection.Enter;
try
  Inc(FRefCount);
  Result := FRefCount;
finally
  CriticalSection.Leave;
end;
end;

function TDataModule1._Release: Integer;
begin

CriticalSection.Enter;
 try
  Dec(FRefCount);
  Result := FRefCount;

  if Result = 0 then
    Destroy
  else
    Self.FLastAccess := Now;
 finally
  CriticalSection.Leave;
  if FRefCount = 1 then
    ReleaseSemaphore(Semaphore, 1, nil);
 end;
end;

constructor TFixedConnectionPool.Create(const PoolSize: Integer = 10;
      const CleanupDelayMinutes: Integer = 5;
      const Timeoutms: LargeInt = 10000);
begin
  FPoolSize := PoolSize;
  FTimeout := Timeoutms;
  Semaphore := CreateSemaphore(nil, PoolSize, PoolSize, '');
  CriticalSection := TCriticalSection.Create;

  SetLength(FPool, PoolSize);

  CleanupThread := TCleanupThread.Create(True, CleanupDelayMinutes);
  with CleanupThread do
  begin
    FreeOnTerminate := True;
    Priority := tpLower;
    FixedConnectionPool := Self;
    Resume;
  end;
end;


constructor TCleanupThread.Create(CreateSuspended: Boolean;
      const CleanupDelayMinutes: Integer);
begin
  // always create suspended
  inherited Create(True); // always create suspended
  FCleanupDelay := CleanupDelayMinutes;
  //Resume if not created suspended
  if not CreateSuspended then
    Resume;
end;

function TFixedConnectionPool.GetConnection: IConnection;
var
  i: Integer;
  DM: TDataModule1;
  WaitResult: Integer;
begin
Result := nil;
WaitResult := WaitForSingleObject(Semaphore, FTimeout);
if WaitResult <> WAIT_OBJECT_0 then
  raise EConnPoolException.Create('Connection pool timeout. '+
    'Cannot obtain a connection');
CriticalSection.Enter;
try
  for i := Low(FPool) to High(FPool) do
    begin

      if FPool[i] = nil then
        begin
          DM := TDataModule1.Create(nil);
          DM.CriticalSection := Self.CriticalSection;
          DM.Semaphore := Self.Semaphore;
          FPool[i] := DM;
          FPool[i].Connection.Connected := True;
          Result := FPool[i];
          Exit;
        end;

      if FPool[i].RefCount = 1 then
        begin
          Result := FPool[i];
          Exit;
        end;
    end;
finally
  CriticalSection.Leave;
end;
end;


destructor TFixedConnectionPool.Destroy;
var
  i: Integer;
begin
  //Free any remaining connections
  CleanupThread.Terminate;
  CriticalSection.Enter;
  try
    for i := Low(FPool) to High(FPool) do
      FPool[i] := nil;
    SetLength(FPool,0);
  finally
    CriticalSection.Leave;
  end;
  CriticalSection.Free;
  //Release the semaphore
  CloseHandle(Semaphore);
  inherited;
end;
end.

ecfisa 03-08-2010 20:04:14

Hola de nuevo Julyus.

Aca esta el código no me dá errores (de compilación);).
Lo reestructuré un poco para mayor legibilidad, no probé la ejecución.
En el código está la línea (1) que soluciona la referencia cruzada entre las clases FixedConnectionPool y TCleanupThread.

Código:

unit Unit2;

interface

uses
  SysUtils, Classes, ADODB, syncobjs, Windows , DB, Dialogs;

type
  IConnection = Interface(IInterface)
    function Connection: TADOConnection;
    function GetRefCount: Integer;
    function GetLastAccess: TDateTime;
    property LastAccess: TDateTime read GetLastAccess;
    property RefCount: Integer read GetRefCount;
  end;

  TFixedConnectionPool = class;  //  (1)  * LINEA ADICIONADA *

  TCleanupThread = class(TThread)
  private
    FCleanupDelay: Integer;
  protected
    CriticalSection: TCriticalSection;
    FixedConnectionPool: TFixedConnectionPool;
    procedure Execute; override;  { FALTABA IMPLEMENTAR }
    constructor Create(CreateSuspended: Boolean;
    const CleanupDelayMinutes: Integer);
  end;

  TFixedConnectionPool = class(TObject)
  private
    FPool: array of IConnection;
    FPoolSize: Integer;
    FTimeout: LargeInt;
    CleanupThread: TCleanupThread;
    Semaphore: THandle;
    CriticalSection: TCriticalSection;
  public
    constructor Create(const PoolSize: Integer = 10;
    const CleanupDelayMinutes: Integer = 5;
    const Timeoutms: LargeInt = 10000); overload;
    destructor Destroy; override;
    function GetConnection: IConnection;
  end;

  TDataModule2 = class(TDataModule, IConnection)
  private
  protected
    FRefCount: Integer;
    FLastAccess: TDateTime;
    CriticalSection: TCriticalSection;
    Semaphore: THandle;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    {IConnection methods}
    function GetLastAccess: TDateTime;  { FALTABA IMPLEMENTAR }
    function GetRefCount: Integer;      { FALTABA IMPLEMENTAR }
  public
    function Connection: TADOConnection; { FALTABA IMPLEMENTAR }
  end;

var
  DataModule2: TDataModule2;

implementation {$R *.dfm}

function TDataModule2.Connection: TADOConnection;
begin
  // IMPLEMENTAR
end;

function TDataModule2.GetLastAccess: TDateTime;
begin
  // IMPLEMENTAR
end;

function TDataModule2.GetRefCount: Integer;
begin
  // IMPLEMENTAR
end;

function TDataModule2._AddRef: Integer;
begin
  CriticalSection.Enter;
  try
  Inc(FRefCount);
  Result := FRefCount;
  finally
  CriticalSection.Leave;
  end;
end;

function TDataModule2._Release: Integer;
begin
CriticalSection.Enter;
 try
  Dec(FRefCount);
  Result := FRefCount;
  if Result = 0 then
    Destroy
  else
    Self.FLastAccess := Now;
 finally
  CriticalSection.Leave;
  if FRefCount = 1 then
    ReleaseSemaphore(Semaphore, 1, nil);
 end;
end;

constructor TFixedConnectionPool.Create(const PoolSize: Integer = 10;
      const CleanupDelayMinutes: Integer = 5;
      const Timeoutms: LargeInt = 10000);
begin
  FPoolSize := PoolSize;
  FTimeout := Timeoutms;
  Semaphore := CreateSemaphore(nil, PoolSize, PoolSize, '');
  CriticalSection := TCriticalSection.Create;
  SetLength(FPool, PoolSize);
  CleanupThread := TCleanupThread.Create(True, CleanupDelayMinutes);
  with CleanupThread do
  begin
    FreeOnTerminate := True;
    Priority := tpLower;
    FixedConnectionPool := Self;
    Resume;
  end;
end;

constructor TCleanupThread.Create(CreateSuspended: Boolean;
      const CleanupDelayMinutes: Integer);
begin
  // always create suspended
  inherited Create(True); // always create suspended
  FCleanupDelay := CleanupDelayMinutes;
  //Resume if not created suspended
  if not CreateSuspended then
    Resume;
end;

function TFixedConnectionPool.GetConnection: IConnection;
var
  i: Integer;
  DM: TDataModule2;
  WaitResult: Integer;
begin
Result := nil;
WaitResult := WaitForSingleObject(Semaphore, FTimeout);
if WaitResult <> WAIT_OBJECT_0 then
  raise Exception.Create('Connection pool timeout. '+  // EConnPoolException: Identificador desconocido
    'Cannot obtain a connection');                    // reemplazo por Exeption
CriticalSection.Enter;
try
  for i := Low(FPool) to High(FPool) do
    begin
      if FPool[i] = nil then
        begin
          DM := TDataModule2.Create(nil);
          DM.CriticalSection := Self.CriticalSection;
          DM.Semaphore := Self.Semaphore;
          FPool[i] := DM;
          FPool[i].Connection.Connected := True;
          Result := FPool[i];
          Exit;
        end;
      if FPool[i].RefCount = 1 then
        begin
          Result := FPool[i];
          Exit;
        end;
    end;
finally
  CriticalSection.Leave;
end;
end;

destructor TFixedConnectionPool.Destroy;
var
  i: Integer;
begin
  //Free any remaining connections
  CleanupThread.Terminate;
  CriticalSection.Enter;
  try
    for i := Low(FPool) to High(FPool) do
      FPool[i] := nil;
    SetLength(FPool,0);
  finally
    CriticalSection.Leave;
  end;
  CriticalSection.Free;
  //Release the semaphore
  CloseHandle(Semaphore);
  inherited;
end;

procedure TCleanupThread.Execute;
begin
  inherited;
  // IMPLEMENTAR
end;

end.

Saludos.

Neftali [Germán.Estévez] 04-08-2010 10:53:31

ecfisa, si utilizas la etiqueta [ DELPHI ] en lugar de la de [ CODE ] te resalta las palabras reservadas.

Sólo es un comentario. No se si lo desconoces o que te va mejor utilizar la de [ CODE ] ;) :confused::confused:

ecfisa 04-08-2010 19:14:24

Cita:

Empezado por Neftali (Mensaje 372536)
ecfisa, si utilizas la etiqueta [ DELPHI ] en lugar de la de [ CODE ] te resalta las palabras reservadas.

Sólo es un comentario. No se si lo desconoces o que te va mejor utilizar la de [ CODE ] ;) :confused::confused:

Hola Neftalí, conozco la etiqueta, pero a veces [delphi] se desconfigura luego de aplicar la vista previa, dejando sólo [+] y todo el texto sin identar.

Quizá sea algún problema de configuración de mi navegador, voy a hacer algo que debería haber hecho antes: Postear el problema en Varios.

Gracias por la observación.


Saludos.

Casimiro Noteví 04-08-2010 20:01:11

Cierto, no sale bien si se le da al botón de vista previa

julyus 06-08-2010 16:02:11

señores muchas gracias
 
hola amigos gracias por sus respuestas me han servido mucho de ayuda pude cuadrar la unidad y bueno recorde cosas que hace rato no hacia en delphi

mil gracias.

julian abreo :D:D:D:D:D:D:D


La franja horaria es GMT +2. Ahora son las 00:34:58.

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