Post by FahdI created my class and implemented in my form using HTTPRIO
object because Indy component antifreeze didn't work
TIdAntiFreeze only works for Indy components. THTTPRio is not an Indy
component.
Post by Fahd"CoInitialize has not been called" when it hit FThread.Resume.
THTTPRio uses ActiveX/COM internally. You have to call CoInitialize/Ex()
(and CoUninitialize()) in any thread that wants to use ActiveX/COM objects
before they can be instantiated.
Worse, ActiveX/COM objects (well, apartment-threaded ones, anyway) are tied
to the thread that instantiates them. So you can't instantiate the THTTPRio
in your main thread and then use it in your worker thread (without custom
marshalling, anyway). It would be better to have the worker thread
instantiate the THTTPRio object directly instead. Then it will be used in
the correct thread context.
Also, your thread's Execute() code is not safe anyway. ShowMessage() is not
thread-safe, and you are not accessing the TEdit in a thread-safe manner.
Post by Fahdam I doing it the right way?
No. Try this code instead:
--- TMyForm ---
private
FThread: TThread;
procedure OnTerminate(Sender:TObject)
procedure TClientForm.LoginButtonClick(Sender: TObject);
begin
FThread := TMyThread.Create(Edit1.Text);
FThread.OnTerminate := OnTerminate;
FThread.Resume;
end;
procedure TClientForm.OnTerminate(Sender: TObject);
begin
FThread := nil;
end;
--- TMyThread ---
type
TMyThread = class(TThread)
private
fText: String;
function GetService: IRealtimeSoap;
protected
procedure Execute; override;
procedure DoTerminate; override;
public
constructor Create(const AText: String); reintroduce;
end;
constructor TMyThread.Create(const AText: String)
begin
inherited Create(True);
FreeOnTerminate = true;
fText := AText;
end;
function GetService: IRealtimeSoap;
var
rio: THTTPRio;
begin
rio := THTTPRio.Create(nil);
// configure rio as needed...
Result := rio as IRealtimeSoap;
end;
procedure TMyThread.Execute;
var
svc: IRealtimeSoap;
begin
CoInitialize(nil);
svc := GetService;
try
Windows.MessageBox(0, PChar(svc.myMethod(fText)), 'myMethod',
MB_OK);
finally
svc := nil;
end;
end;
procedure TMyThread.DoTerminate;
begin
CoUninitialize;
inherited;
end;
Gambit