Hi, jaja2005,
I've made a little project for you (but I haven't finished the tests yet). See if it is something like this that you want.
To compile it, create the files which name are surrounding by '*******'.
Regards,
Douglas.
************************
TestThread.dpr
***********************
program ThreadTest;
uses
Forms,
Mainform in 'Mainform.pas' {FMainform},
BossWorkerThreads in 'BossWorkerThreads.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TFMainform, FMainform);
Application.Run;
end.
********************************
Mainform.dfm
********************************
object FMainform: TFMainform
Left = 192
Top = 103
Width = 696
Height = 480
Caption = 'Thread Test'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 32
Top = 24
Width = 113
Height = 25
Caption = 'Create Boss Thread'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 24
Top = 64
Width = 257
Height = 321
TabOrder = 1
end
object Button2: TButton
Left = 328
Top = 24
Width = 169
Height = 25
Caption = 'Resume Boss Thread'
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 168
Top = 24
Width = 153
Height = 25
Caption = 'Suspend Boss Thread'
TabOrder = 3
OnClick = Button3Click
end
end
********************************
Mainform.pas
********************************
unit Mainform;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFMainform = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FMainform: TFMainform;
implementation
{$R *.dfm}
uses BossWorkerThreads;
var
b: TBossThread;
procedure TFMainform.Button1Click(Sender: TObject);
begin
b := TBossThread.Create(3, Self);
end;
procedure TFMainform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if b <> nil then
b.Terminate;
end;
procedure TFMainform.Button3Click(Sender: TObject);
begin
if b <> nil then
b.Suspend;
end;
procedure TFMainform.Button2Click(Sender: TObject);
begin
if b <> nil then
b.Resume;
end;
end.
****************************
BossWorkerThreads.pas
****************************
unit BossWorkerThreads;
interface
uses
SysUtils, Classes, SyncObjs, Windows, Mainform, Dialogs;
type
TWorkerThread = class;
TWorkerThreadResult = record
WorkerThread: TWorkerThread;
WorkerThreadID: Cardinal;
Result: Boolean;
end;
PWorkerThreadResult = ^TWorkerThreadResult;
TBossThread = class(TThread)
private
FFMainform: TFMainform;
FSemaphore: Integer;
FEvent: TEvent;
FWorkerResults: TThreadList;
FWorkerThreadList: TList;
{ Private declarations }
protected
procedure Execute; override;
function SpawnThread: Boolean;
public
constructor Create(Semaphore: Integer; FMainform: TFMainform);
destructor Destroy; override;
procedure AddWorkerThreadResult(WorkerThreadResult: TWorkerThreadResult);
property Event: TEvent read FEvent;
end;
TWorkerThread = class(TThread)
private
FBossThread: TBossThread;
{ Private declarations }
function DoTest(TestFlag: Boolean): Boolean;
protected
procedure Execute; override;
public
constructor Create(BossThread: TBossThread);
end;
implementation
{ TBossThread }
procedure TBossThread.AddWorkerThreadResult(WorkerThreadResult: TWorkerThreadResult);
begin
with FWorkerResults do
begin
with LockList do
try
Add(@WorkerThreadResult);
FEvent.SetEvent;
finally
UnlockList;
end;
end;
end;
constructor TBossThread.Create(Semaphore: Integer; FMainform: TFMainform);
begin
inherited Create(True);
FWorkerResults := TThreadList.Create;
FWorkerThreadList := TList.Create;
FEvent := TEvent.Create(nil, False, False, '');
FSemaphore := Semaphore;
FFMainform := FMainform;
FreeOnTerminate := True;
Resume;
end;
destructor TBossThread.Destroy;
var
i: Integer;
begin
for i := 0 to FWorkerThreadList.Count - 1 do
TWorkerThread(FWorkerThreadList[i]).Terminate;
FWorkerResults.Free;
FEvent.Free;
inherited;
end;
procedure TBossThread.Execute;
var
List: TList;
WorkerThreadResult: PWorkerThreadResult;
FAvailableThreads: Boolean;
begin
while not Terminated do
begin
repeat
FAvailableThreads := SpawnThread
until not FAvailableThreads;
List := FWorkerResults.LockList;
try
if List.Count > 0 then
begin
WorkerThreadResult := List.Items[0];
FFMainform.Memo1.Lines.Add('ThreadID = ' + IntToStr(WorkerThreadResult^.WorkerThreadID) + ', ' +
'Result = ' + IntToStr(Integer(WorkerThreadResult^.Result)));
FWorkerResults.Remove(WorkerThreadResult);
end
finally
FWorkerResults.UnlockList;
if List.Count = 0 then
begin
FEvent.ResetEvent;
FEvent.WaitFor(INFINITE);
end;
end;
end;
end;
function TBossThread.SpawnThread: Boolean;
begin
Result := False;
if FSemaphore > 0 then
begin
FWorkerThreadList.Add(TWorkerThread.Create(Self));
Dec(FSemaphore);
Result := True;
end;
end;
{ TWorkerThread }
constructor TWorkerThread.Create(BossThread: TBossThread);
begin
inherited Create(True);
FBossThread := BossThread;
FreeOnTerminate := True;
Resume;
end;
function TWorkerThread.DoTest(TestFlag: Boolean): Boolean;
begin
Result := not TestFlag;
end;
procedure TWorkerThread.Execute;
var
WorkerThreadResult: TWorkerThreadResult;
TestFlag: Boolean;
begin
TestFlag := False;
with WorkerThreadResult do
begin
WorkerThread := Self;
WorkerThreadID := ThreadID;
end;
while not Terminated do
begin
TestFlag := DoTest(TestFlag);
WorkerThreadResult.Result := TestFlag;
FBossThread.AddWorkerThreadResult(WorkerThreadResult);
end;
end;
end.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313:





by: Geert_GruwezPosted on 2009-07-07 at 19:27:09ID: 24800111
m2c
a thread shouldn't update anything in the VCL if not using synchronize or lock
instead you should let the form update itself (not in a thread)
rather give the form the information it needs in a object instance and send it a message the data changed