Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. TEmulatorThread = class(TThread)
  2. private
  3. FOwnerHandle: THandle;
  4. protected
  5. procedure Execute; override;
  6. property OwnerHandle: THandle read FOwnerHandle;
  7. public
  8. constructor Create(OwnerHandle: THandle);
  9. end;
  10.  
  11. constructor TEmulatorThread.Create(OwnerHandle: THandle);
  12. begin
  13. FOwnerHandle := OwnerHandle;
  14. inherited Create(False);
  15. end;
  16.  
  17. procedure TEmulatorThread.Execute;
  18. var
  19. msg: TMsg;
  20. begin
  21. with TDataBus.Create(OwnerHandle) do
  22. try
  23. while not Terminated do
  24. begin
  25. try
  26. while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
  27. if msg.message = WM_TERMINATE then
  28. Terminate
  29. else
  30. DispatchMessage(msg);
  31. if not Terminated then
  32. WaitMessage;
  33. except
  34. PostMessage(OwnerHandle, WM_EXCEPTION, Integer(AcquireExceptionObject), 0);
  35. end;
  36. end;
  37. finally
  38. Free;
  39. end;
  40. end;
  41.  
  42. constructor TEmulator.Create;
  43. begin
  44. inherited;
  45. FDataBus := nil;
  46. FWindowHandle := AllocateHWnd(WindowProc);
  47. FThread := TEmulatorThread.Create(FWindowHandle);
  48. end;
  49.  
  50. destructor TEmulator.Destroy;
  51. begin
  52. PostThreadMessage(FThread.ThreadID, WM_TERMINATE, 0, 0);
  53. FThread.WaitFor;
  54. FThread.Free;
  55. DeallocateHWnd(FWindowHandle);
  56. inherited;
  57. end;
  58.  
  59. procedure TEmulator.WindowProc(var Message: TMessage);
  60. begin
  61. with Message do
  62. case msg of
  63. WM_DATABUS_NOTIFY:
  64. case WParam of
  65. DB_CREATED:
  66. FDataBus := TDataBus(LParam);
  67. DB_DESTROYING:
  68. FDataBus := nil;
  69. DB_OBJECT_ADDED:
  70. ;
  71. DB_OBJECT_DELETED:
  72. ;
  73. end;
  74. WM_OBJECT_NOTIFY:
  75. ;
  76. WM_EXCEPTION:
  77. begin
  78. raise Exception(WParam);
  79. end;
  80. else
  81. DefWindowProc(FWindowHandle, msg, WParam, LParam);
  82. end;
  83. end;
  84.  
  85. procedure TBONDataBus.AfterConstruction;
  86. begin
  87. PostMessage(FOwnerHandle, WM_DATABUS_NOTIFY, DB_CREATED, Integer(Self));
  88. end;
  89.  
  90. procedure TBONDataBus.BeforeDestruction;
  91. begin
  92. PostMessage(FOwnerHandle, WM_DATABUS_NOTIFY, DB_DESTROYING, 0);
  93. inherited;
  94. end;
Add Comment
Please, Sign In to add comment