Using a COM Object in different threads?
Posted on 2003-12-11
I'm writing a multi-tier application using a TRemoteDataModule in my app server.
In the client I use a TDComConnection to connect to the server's IAppServer interface.
So far all works fine when I call any method of the app server in my main thread.
But since some calls take a significant amount of time, I wanted to make these calls in
a separate thread. (At first I just displayed a little dialog with some "Please wait..." message
in it, so that I didn't need a separate thread. But this approach is not very satisfying for me)
So I wrote a new thread and placed the calls into it. After some time I was able to call the needed
methods from this thread (BTW: Why do I have to call CoInitialize in this thread again? )
The problem is now, I'm also using a TClientDataSet in my client. And this DataSet is controlled by the
main thread. Whenever anything (be it the ClientDataSet or some other method ) tries to call
the app server now, I get an error message telling me that this interface has already been marshalled
for another thread.
So it seems that I can only call the IAppServer interface from *either* the main thread *or* the
separate thread. Is there a workaround so I can call it from both threads?