Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i use COM+ component from customized thread

Status
Not open for further replies.

IPOz

Programmer
Jul 11, 2001
109
0
0
CN
Hi,friends
i want to use COM+ from a thread however it always reports error.The following is a delphi code snippet:
type
TAlarmReader = class(TThread)
public
procedure getData;
procedure execute; override;
end;

TfrmMain = class(TForm)
dcomAlarm: TDCOMConnection;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FAlarmReader : TAlarmReader;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TAlarmReader.getData;
begin
with frmMain.dcomAlarm do
try
Connected := True; <===== here will raise error
finally
Connected := False;
end;
end;

procedure TAlarmReader.execute;
begin
while (not Terminated) do begin
Synchronize(getData);
end;
end;


procedure TfrmMain.Button1Click(Sender: TObject);
begin
FAlarmReader := TAlarmReader.Create(True);
FAlarmReader.Priority := tpLower;
FAlarmReader.Resume;
end;

end.

the error message is strange !

Best Regards! IPO_z@cmmail.com
Garbage in,Garbage out
 
Hi,
You can
a) try initializing com with coinitialize(..) inside of the getdata method and counitialize finishing the method.
b) try creating the connection inside of the getdata method.
(this probably works, if it does, i think it's because you can't easily share interfaces between threads except if your program is an MTA)

Hope by helpful
Das
 
Thanks for your response!
I have changed getData as below:
...
procedure TAlarmReader.getData;
begin
with frmMain.dcomAlarm do
try
CoInitialize(nil);
Connected := True; <===== here will raise error
//...........
finally
Connected := False;
Couninitialize();
end;
end;
...
But i got the message &quot;The message filer displays that the application is being used&quot;

Does MTA meam Multiple Thread Apartment? If so,how do i change a standalone application to MTA?

Best Regards! IPO_z@cmmail.com
Garbage in,Garbage out
 
Hi
This it's something that i do your for test and i don't thinks that's the rigth way, but you can try, if i remember correctly i do this :

include in your dpr
comobj,
and before application.initialize
set
CoInitFlags := 0;

this works for me but i thinks that the rigth way of do it it's trhu the freeglobalinterface table.

Any way, why don't you try creating your component conection inside getdata?

DAS




 
I have added CoInitFlags := 0; before Application.Initialize;but it still doesnot work steadily.(only passed at the first time)

>>why don't you try creating your component connection inside getdata?
You mean i need to use getData() as below:

procedure TAlarmReader.getData;
begin
Coinitialize(nil);
with TCOMConnection.Create(nil) do
try
ServerName := 'IH_DO.alarm';
Connected := True;
finally
Connected := False;
Free;
Couninitialize();
end;
end;
If so,it always doesnot work!

BTW,my COM+ component is set to Apartment thread mode.

Regards! IPO_z@cmmail.com
Garbage in,Garbage out
 
Hi,
I use this function to create a CC:

function CreateCC(aComponentServer,
ServerGuid: String): TComponentConnection;
begin
result := TComponentConnection.Create(Nil);
try
result.ComputerName:= aComponentServer;
result.ConnectionType:=ctDCOM;
result.ServerGUID:= ServerGuid; (THE GUID OF YOUR COM+ COMPONENT)
Except
result := nil;
end;
end;

This it's working now for me and i create the cc and use it in a thread.
Try with this, if it does'nt work may be the the problem it's at other level (i don't know).

Regards

(Excuse me if me english it's bad but it's not my normal lenguaje)
 
Sorry! i can NOT compile using TComponentConnection.Becuause i donot know which unit it is in .


BTW,i know what you said in English.

Best Regards! IPO_z@cmmail.com
Garbage in,Garbage out
 
Sorry the mistake and delay
change TComponentConnection by TDCOMConnection

so

function CreateCC(aComponentServer,
ServerGuid: String): TDCOMConnection;
begin
result := TDCOMConnection.Create(Nil);
try
result.ComputerName:= aComponentServer;
result.ServerGUID:= ServerGuid; (THE GUID OF YOUR COM+ COMPONENT)
Except
result := nil;
end;
end

you can use ServerName instead of guid;

DAS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top