| 1 | unit UResetableThread;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, syncobjs;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 | TResetableThread = class;
|
|---|
| 12 |
|
|---|
| 13 | { TResetableThreadExecute }
|
|---|
| 14 |
|
|---|
| 15 | TResetableThreadExecute = class(TThread)
|
|---|
| 16 | Parent: TResetableThread;
|
|---|
| 17 | procedure Execute; override;
|
|---|
| 18 | end;
|
|---|
| 19 |
|
|---|
| 20 | { TResetableThread }
|
|---|
| 21 |
|
|---|
| 22 | TResetableThread = class
|
|---|
| 23 | private
|
|---|
| 24 | StartEvent: TEvent;
|
|---|
| 25 | StopEvent: TEvent;
|
|---|
| 26 | public
|
|---|
| 27 | Thread: TResetableThreadExecute;
|
|---|
| 28 | Stopped: Boolean;
|
|---|
| 29 | procedure Execute; virtual; abstract;
|
|---|
| 30 | procedure Stop;
|
|---|
| 31 | procedure Start;
|
|---|
| 32 | procedure WaitForStart;
|
|---|
| 33 | procedure WaitForStop;
|
|---|
| 34 | constructor Create;
|
|---|
| 35 | destructor Destroy; override;
|
|---|
| 36 | end;
|
|---|
| 37 |
|
|---|
| 38 | implementation
|
|---|
| 39 |
|
|---|
| 40 | { TResetableThread }
|
|---|
| 41 |
|
|---|
| 42 | procedure TResetableThread.Stop;
|
|---|
| 43 | begin
|
|---|
| 44 | Stopped := True;
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | procedure TResetableThread.Start;
|
|---|
| 48 | begin
|
|---|
| 49 | if Stopped then StartEvent.SetEvent;
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | procedure TResetableThread.WaitForStart;
|
|---|
| 53 | var
|
|---|
| 54 | WaitResult: TWaitResult;
|
|---|
| 55 | begin
|
|---|
| 56 | repeat
|
|---|
| 57 | WaitResult := StartEvent.WaitFor(1000);
|
|---|
| 58 | until WaitResult <> wrTimeout;
|
|---|
| 59 | if WaitResult = wrError then
|
|---|
| 60 | raise Exception.Create('WaitFor error');
|
|---|
| 61 | end;
|
|---|
| 62 |
|
|---|
| 63 | procedure TResetableThread.WaitForStop;
|
|---|
| 64 | begin
|
|---|
| 65 | while StopEvent.WaitFor(1000) = wrTimeout do ;
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | constructor TResetableThread.Create;
|
|---|
| 69 | begin
|
|---|
| 70 | Stopped := True;
|
|---|
| 71 | StartEvent := TEvent.Create(nil, False, False, '');
|
|---|
| 72 | StopEvent := TEvent.Create(nil, False, False, '');
|
|---|
| 73 | Thread := TResetableThreadExecute.Create(True);
|
|---|
| 74 | Thread.Parent := Self;
|
|---|
| 75 | Thread.Resume;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | destructor TResetableThread.Destroy;
|
|---|
| 79 | begin
|
|---|
| 80 | Stop;
|
|---|
| 81 | WaitForStop;
|
|---|
| 82 | Thread.Destroy;
|
|---|
| 83 | StartEvent.Destroy;
|
|---|
| 84 | StopEvent.Destroy;
|
|---|
| 85 | inherited Destroy;
|
|---|
| 86 | end;
|
|---|
| 87 |
|
|---|
| 88 | { TResetableThreadExecute }
|
|---|
| 89 |
|
|---|
| 90 | procedure TResetableThreadExecute.Execute;
|
|---|
| 91 | begin
|
|---|
| 92 | while not Terminated do begin
|
|---|
| 93 | Parent.WaitForStart;
|
|---|
| 94 | Parent.Stopped := False;
|
|---|
| 95 | Parent.Execute;
|
|---|
| 96 | Parent.Stopped := True;
|
|---|
| 97 | Parent.StopEvent.SetEvent;
|
|---|
| 98 | end;
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | end.
|
|---|
| 102 |
|
|---|