Advertisement
TLama

Untitled

Jan 8th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.64 KB | None | 0 0
  1. type
  2.   TSomeThread = class(TThread)
  3.   private
  4.     FAbortEvent: THandle;
  5.     FContinueEvent: THandle;
  6.   protected
  7.     procedure Execute; override;
  8.     procedure TerminatedSet; override;
  9.   public
  10.     constructor Create; reintroduce;
  11.     destructor Destroy; override;
  12.     procedure ContinueExecution;
  13.   end;
  14.  
  15. implementation
  16.  
  17. constructor TSomeThread.Create;
  18. begin
  19.   inherited Create(False);
  20.   FAbortEvent := CreateEvent(nil, False, False, nil);
  21.   FContinueEvent := CreateEvent(nil, False, False, nil);
  22. end;
  23.  
  24. destructor TSomeThread.Destroy;
  25. begin
  26.   CloseHandle(FContinueEvent);
  27.   CloseHandle(FAbortEvent);
  28.   inherited Destroy;
  29. end;
  30.  
  31. procedure TSomeThread.Execute;
  32. var
  33.   WaitResult: DWORD;
  34.   WaitEvents: array[0..1] of THandle;
  35. begin
  36.   // some actions before pause...
  37.   // prepare the waiting event array
  38.   WaitEvents[0] := FAbortEvent;
  39.   WaitEvents[1] := FContinueEvent;
  40.   // and block the thread for infinite time until you signal one of those events;
  41.   // that may happen if you (or the application) calls Terminate, or if you call
  42.   // ContinueExecution method
  43.   case WaitForMultipleObjects(2, @WaitEvents, False, INFINITE) of
  44.     WAIT_OBJECT_0: ;      // Terminate has been called in this branch
  45.     WAIT_OBJECT_0 + 1:
  46.     begin
  47.       // ContinueExecution has been called in this branch, so here you can continue
  48.       // with your processing
  49.     end;
  50.     WAIT_FAILED: ;        // something unexpected happened; GetLastError tells you what
  51.   end;
  52. end;
  53.  
  54. procedure TSomeThread.TerminatedSet;
  55. begin
  56.   inherited;
  57.   SetEvent(FAbortEvent);
  58. end;
  59.  
  60. procedure TSomeThread.ContinueExecution;
  61. begin
  62.   SetEvent(FContinueEvent);
  63. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement