|
|
От: |
AMogil
|
|
| Дата: | 25.09.04 18:27 | ||
| Оценка: | |||
Класс:
TMainClass = class(TInterfacedObject)
private
FCommandText: WideString;
FCriticalSection: TCriticalSection;
FThreadClass: TThreadClass;
public
constructor Create; virtual;
destructor Destroy; override;
function GetCommandText: WideString; overload;
procedure SetCommandText(const Value: WideString);
end;
...
constructor TMainClass.Create;
begin
inherited;
FCriticalSection := TCriticalSection.Create;
FThreadClass := TThreadClass.Create(Self);
SetSQLCommandText('ddd');
FThreadClass.Resume;
end;
function TMainClass.GetCommandText: WideString;
begin
FCriticalSection.Enter;
try
Result := FCommandText;
finally
FCriticalSection.Leave;
end;
end;
procedure TMainClass.SetCommandText(const Value: WideString);
begin
FCriticalSection.Enter;
try
FCommandText := Value;
finally
FCriticalSection.Leave;
end;
end;
Нить:
TThreadClass = class(TThread)
protected
FOwner: TObject;
procedure Execute; override;
public
constructor Create(Owner: TObject); virtual;
destructor Destroy; override;
end;
...
constructor TThreadClass.Create(Owner: TObject);
begin
inherited Create(True);
FOwner := Owner;
FreeOnTerminate := False;
end;
procedure TDatabaseConnectionThread.Execute;
var
S: WideString;
begin
S := TMainClass(FOwner).GetCommandText;
...
end;Critical sections must be global in scope so that they are available to all threads