Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TEmulatorThread = class(TThread)
- private
- FOwnerHandle: THandle;
- protected
- procedure Execute; override;
- property OwnerHandle: THandle read FOwnerHandle;
- public
- constructor Create(OwnerHandle: THandle);
- end;
- constructor TEmulatorThread.Create(OwnerHandle: THandle);
- begin
- FOwnerHandle := OwnerHandle;
- inherited Create(False);
- end;
- procedure TEmulatorThread.Execute;
- var
- msg: TMsg;
- begin
- with TDataBus.Create(OwnerHandle) do
- try
- while not Terminated do
- begin
- try
- while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
- if msg.message = WM_TERMINATE then
- Terminate
- else
- DispatchMessage(msg);
- if not Terminated then
- WaitMessage;
- except
- PostMessage(OwnerHandle, WM_EXCEPTION, Integer(AcquireExceptionObject), 0);
- end;
- end;
- finally
- Free;
- end;
- end;
- constructor TEmulator.Create;
- begin
- inherited;
- FDataBus := nil;
- FWindowHandle := AllocateHWnd(WindowProc);
- FThread := TEmulatorThread.Create(FWindowHandle);
- end;
- destructor TEmulator.Destroy;
- begin
- PostThreadMessage(FThread.ThreadID, WM_TERMINATE, 0, 0);
- FThread.WaitFor;
- FThread.Free;
- DeallocateHWnd(FWindowHandle);
- inherited;
- end;
- procedure TEmulator.WindowProc(var Message: TMessage);
- begin
- with Message do
- case msg of
- WM_DATABUS_NOTIFY:
- case WParam of
- DB_CREATED:
- FDataBus := TDataBus(LParam);
- DB_DESTROYING:
- FDataBus := nil;
- DB_OBJECT_ADDED:
- ;
- DB_OBJECT_DELETED:
- ;
- end;
- WM_OBJECT_NOTIFY:
- ;
- WM_EXCEPTION:
- begin
- raise Exception(WParam);
- end;
- else
- DefWindowProc(FWindowHandle, msg, WParam, LParam);
- end;
- end;
- procedure TBONDataBus.AfterConstruction;
- begin
- PostMessage(FOwnerHandle, WM_DATABUS_NOTIFY, DB_CREATED, Integer(Self));
- end;
- procedure TBONDataBus.BeforeDestruction;
- begin
- PostMessage(FOwnerHandle, WM_DATABUS_NOTIFY, DB_DESTROYING, 0);
- inherited;
- end;
Add Comment
Please, Sign In to add comment