Thursday, July 03, 2008

OmniThreadLibrary Example #4a: Bidirectional communication, with objects

imageThis is a variation of Example #4 that uses TOmniWorker object instead of IOmniWorker interface. Code is stored in folder tests\6_TwoWayHello_with_object_worker.

I strongly suggest that you look at Example #4 first, as this post only lists the differences between those two examples.

Firstly, we don't need TAsyncHello.Initialize. We'll set initial message text in the constructor. [Admittedly, this is something that can also be done when using IOmniWorker approach.] Message handlers are identical to the Example #4.

  TAsyncHello = class(TOmniWorker)
strict private
aiMessage: string;
public
constructor Create(const initialMessage: string);
procedure OMChangeMessage(var msg: TOmniMessage); message MSG_CHANGE_MESSAGE;
procedure OMSendMessage(var msg: TOmniMessage); message MSG_SEND_MESSAGE;
end;
constructor TAsyncHello.Create(const initialMessage: string);
begin
aiMessage := initialMessage;
end;

Secondly, we have to tweak the task creation.

procedure TfrmTestOTL.actStartHelloExecute(Sender: TObject);
begin
FHelloTask :=
OmniTaskEventDispatch1.Monitor(CreateTask(TAsyncHello.Create('Hello'), 'Hello')).
SetIdle(1000, MSG_SEND_MESSAGE).
SetParameter('Delay', 1000).
FreeOnTerminate.
Run;
end;

TAsyncHello object can now be passed directly to the CreateTask method. There is no need to set the Message parameter. Also new is the FreeOnTerminate call which tells the task manager to destroy the worker object when task is terminated. Alternatively, you can store away the worker object and destroy it "manually" after the FHelloTask is terminated.


The rest of the code does not differ from the Example #4.

Labels: , , , , , ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home