 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
William Buchanan Guest
|
Posted: Thu Jul 17, 2003 9:49 am Post subject: Using a TClientDataset in a thread |
|
|
Hi folks
I have an app with a data module. On the data module is a TClientDataSet
connected to a TDCOMConnection. I want to create a thread which will open
the clientdataset.
The trouble is that I get the error "The application called an interface
that was marshalled for a different thread".
I have looked on numerous sites for a way around this, but none of them seem
to be doing exactly what I am doing and so I can't get it working.
Does anyone have an example of how to do this or any advice?
Regards
Will
|
|
| Back to top |
|
 |
William Buchanan Guest
|
Posted: Fri Jul 18, 2003 8:28 am Post subject: Re: Using a TClientDataset in a thread |
|
|
Hi Wayne.
I have seen sites mention that you can marshal these connections for use in
threads (although no example did exactly what I was looking for). Is this
possible?
Also, if this is not possible this means that I will need to have the main
thread open the cds which can mean a long time that the app will be frozen
which is what I am trying to avoid - can you think of any other way around
this?
Regards
Will
"Wayne Niddery [TeamB]" <wniddery (AT) chaff (DOT) aci.on.ca> wrote
| Quote: | William Buchanan wrote:
I have an app with a data module. On the data module is a
TClientDataSet connected to a TDCOMConnection. I want to create a
thread which will open the clientdataset.
The trouble is that I get the error "The application called an
interface that was marshalled for a different thread".
This indicates the CDS or connection are already in use in the main
thread.
The separate thread needs its own connection and CDS.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
Powered by Delphi and IB: http://www.logicfundamentals.com/RadBooks.html
"Democracy, without that guarantee of liberty, is merely a method of
selecting tyrants." - Alan Nitikman
|
|
|
| Back to top |
|
 |
vavan Guest
|
Posted: Fri Jul 18, 2003 9:21 am Post subject: Re: Using a TClientDataset in a thread |
|
|
On Fri, 18 Jul 2003 09:28:58 +0100, "William Buchanan"
<wbuchanan_NO_SPAM (AT) eims (DOT) biz> wrote:
| Quote: | I have seen sites mention that you can marshal these connections for use in
threads (although no example did exactly what I was looking for). Is this
possible?
|
yes it is. You don't need to supply separate thread with its own
connection and CDS (as Wayne says) just marshal existing interface
correctly. And what exactly are you trying to achieve?
--
Vladimir Ulchenko aka vavan
|
|
| Back to top |
|
 |
William Buchanan Guest
|
Posted: Fri Jul 18, 2003 3:07 pm Post subject: Re: Using a TClientDataset in a thread |
|
|
Hi Vavan
Thanks for the reply
I am trying to have a thread which will open the CDS - this should allow the
app to run as normal while this is happening. This is why I need to pass the
CDS into my thread and allow the thread to open it.
I have seen examples on how to marshal interfaces but I couldn't get any of
them to work.
Regards
Will
"vavan" <vavan (AT) santel (DOT) ru> wrote
| Quote: | On Fri, 18 Jul 2003 09:28:58 +0100, "William Buchanan"
[email]wbuchanan_NO_SPAM (AT) eims (DOT) biz[/email]> wrote:
I have seen sites mention that you can marshal these connections for use
in
threads (although no example did exactly what I was looking for). Is this
possible?
yes it is. You don't need to supply separate thread with its own
connection and CDS (as Wayne says) just marshal existing interface
correctly. And what exactly are you trying to achieve?
--
Vladimir Ulchenko aka vavan
|
|
|
| Back to top |
|
 |
vavan Guest
|
Posted: Mon Jul 21, 2003 6:29 am Post subject: Re: Using a TClientDataset in a thread |
|
|
On Fri, 18 Jul 2003 16:07:15 +0100, "William Buchanan"
<wbuchanan_NO_SPAM (AT) eims (DOT) biz> wrote:
| Quote: | I am trying to have a thread which will open the CDS - this should allow the
app to run as normal while this is happening. This is why I need to pass the
CDS into my thread and allow the thread to open it.
|
you may need to create smth like example below..
unit TThreadedCDSOpenUnit;
interface
uses
Classes, DBClient, ActiveX, Windows, SysUtils, Midas;
TThreadedCDSOpen = class(TThread)
private
FCDS:TList;
onException : TNotifyEventWithException;
onSuccess : TNotifyEvent;
Exc : Exception;
App : IAppServer;
procedure DoOnException;
procedure DoOnSuccess;
protected
procedure Execute; override;
public
FStream:pointer;
....
end;
implementation
procedure TThreadedCDSOpen.Execute;
var
Res:HResult;
i :integer;
begin
try
Res:=CoInitialize(nil);
if Res<>S_OK then raise Exception.Create('CoInitialize failed in
TThreadedCDSOpen.Execute. '+SysErrorMessage(Res));
try
Res:=CoGetInterfaceAndReleaseStream(IStream(FStream),IID_IAppServer,App);
if Res<>S_OK then raise
Exception.Create('CoGetInterfaceAndReleaseStream failed in
TThreadedCDSOpen.Execute. '+SysErrorMessage(Res));
for i:=0 to FCDS.Count-1 do
with TClientDataSet(FCDS.Items[i]) do begin
AppServer:=App;
Open;
end;
Synchronize(DoOnSuccess);
CoUnInitialize;
except begin
CoUnInitialize;
raise;
end;
end;
except on E:Exception do begin
Exc:=E;
Synchronize(DoOnException);
end;
end;
FStream:=nil;
end;
--
Vladimir Ulchenko aka vavan
|
|
| Back to top |
|
 |
vavan Guest
|
Posted: Mon Jul 21, 2003 9:20 am Post subject: Re: Using a TClientDataset in a thread |
|
|
On Mon, 21 Jul 2003 09:57:10 +0100, "William Buchanan"
<wbuchanan_NO_SPAM (AT) eims (DOT) biz> wrote:
| Quote: | I copied and pasted your code, added a couple of lines and it works!
Many thanks!
|
glad you've found it useful. It was just a demo :)
--
Vladimir Ulchenko aka vavan
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|