I'm new to multithreading, but not a complete newbie. I need to make a web service call in a workflow.
In the main stream, I have a form (TForm) with a private data member (private string), to which only the worker stream will write (I pass a pointer to it in the stream until it resumes). When the workflow completed its webservice call and wrote the resulting xml response to the private member of the form, the workflow uses PostMessage to send the message to the form handle (which I also passed to the stream before it resumes).
interface const WM_WEBSERVCALL_COMPLETE = WM_USER + 1; type TWebServiceResponseXML = string; PWebServiceResponseXML = ^TWebServiceResponseXML; TMyForm = class(TForm) ... private ... fWorkerThreadID: Cardinal; fWebServiceResponseXML: TWebServiceResponseXML; public ... procedure StartWorkerThread; procedure OnWebServiceCallComplete(var Message: TMessage); Message WM_WEBSERVCALL_COMPLETE; end; TMyThread = class(TThread) private protected procedure Execute; override; public SenderHandle: HWnd; RequestXML: string; ResponseXML: string; IMyService: IService; PResponseXML: PWebServiceResponseXML; end; implementation procedure TMyForm.StartWorkerThread; var MyWorkerThread: TMyThread; begin MyWorkerThread := TMyThread.Create(True); MyWorkerThread.FreeOnTerminate := True; MyWorkerThread.SenderHandle := self.Handle; MyWorkerThread.RequestXML := ComposeRequestXML; MyWorkerThread.PResponseXML := ^fWebServiceResponseXML; MyWorkerThread.Resume; end; procedure TMyForm.OnWebServiceCallComplete(var Message: TMessage); begin // Do what you want with the response xml string in fWebServiceResponseXML end; procedure TMyThread.Execute; begin inherited; CoInitialize(nil); try IMyService := IService.GetMyService(URI); ResponseXML := IMyService.Search(RequestXML); PResponseXML := ResponseXML; PostMessage(SenderHandle, WM_WEBSERVCALL_COMPLETE, 0, 0); finally CoUninitialize; end; end;
This works fine, but now I want to do the same from a datamodule (which does not have a Handle) ... so I would really appreciate some useful code that complements the working model I have.
EDIT
What I really want is a code (if possible) that will allow me to replace the string
MyWorkerThread.SenderHandle := self.Handle;
from
MyWorkerThread.SenderHandle := GetHandleForThisSOAPDataModule;
source share