Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

Calling a COM object from a different thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Moreno - 07 Jun 2005 15:38 GMT
Hello,

I have the following simple Architecture:

Server in CSharp    <===>    COM DLL (Apartment threading)   <====>  
DB

When I start the server I need to load several COM objects from the DB.
This lasts between 5 and 10 minutes.

I would like to do this in a worker thread to prevent the server to
hang.

So I think that I should:
- Create my objects in my main STA thread (should I change that to MTA
instead ???)
- Marshal the interfaces into another thread (MTA)
- Call the DB Load method from this worker thread.

The issue is that I have absolutely no idea whether it is the correct
way to do it in .Net and if it is on how to do it in CSharp!

Can anyone help please with an example?

Alternatively here are a few questions:
- How do I marshal the COM interface (do I need to do it through unsafe
code?)
- How do I create a worker MTA thread?
- Can the main application thread be an MTA instead of an STA (there is
a GUI)?

Many thanks in advance for any help.

Signature

----
http://michael.moreno.free.fr/

Willy Denoyette [MVP] - 07 Jun 2005 19:25 GMT
> Hello,
>
[quoted text clipped - 26 lines]
>
> Many thanks in advance for any help.

No, you should never init a UI thread for MTA, nor should you create
instances of STA objects on MTA threads.
All you have to do is create a background thread and initialize it to enter
a STA before you start the thread. After that you simply create an instance
of the COM object and call your methods on it.

           Thread t = new Thread(.....
           t.ApartmentState = ApartmentState.STA;
           t.IsBackground = true;
           t.Start();

Just take care when updating the UI from this thread, you have to use
Control.Invoke or Control.BeginInvoke to do so.

Willy.
Michael Moreno - 08 Jun 2005 08:14 GMT
> No, you should never init a UI thread for MTA, nor should you create
> instances of STA objects on MTA threads.

OK, thanks

> All you have to do is create a background thread and initialize it to enter a
> STA before you start the thread. After that you simply create an instance of
[quoted text clipped - 4 lines]
>             t.IsBackground = true;
>             t.Start();

Thank you. Do you really mean that from this thread I can access my COM
object that were created in the main thread without having to marshall
the interface?

> Just take care when updating the UI from this thread, you have to use
> Control.Invoke or Control.BeginInvoke to do so.

Yes thanks.

Signature

----
http://michael.moreno.free.fr/

Willy Denoyette [MVP] - 08 Jun 2005 16:02 GMT
Inline **

Willy.

>> No, you should never init a UI thread for MTA, nor should you create
>> instances of STA objects on MTA threads.
[quoted text clipped - 13 lines]
> object that were created in the main thread without having to marshall the
> interface?

** Yes you can, but that doesn't mean you should create the object on the
Main UI thread, if you only intend to call methods from the background
thread you better create the COM object on this thead.
If you are calling methods on both you can create an instance on both as
well, so each will have his own instance, no marshaling needed.
If however, you create an instance on the Main thread and pass a reference
to it to the background thread, the CLR will automagically marshall the
Interface pointer for you (tout gratuit).

>> Just take care when updating the UI from this thread, you have to use
>> Control.Invoke or Control.BeginInvoke to do so.
>
> Yes thanks.
Michael Moreno - 08 Jun 2005 17:00 GMT
> ** Yes you can, but that doesn't mean you should create the object on the
> Main UI thread, if you only intend to call methods from the background thread
> you better create the COM object on this thead.

yes I agree.

> If however, you create an instance on the Main thread and pass a reference to
> it to the background thread, the CLR will automagically marshall the
> Interface pointer for you (tout gratuit).

That is the best news I have heard for a long time!!!!

Thanks a lot for your help.

Signature

----
http://michael.moreno.free.fr/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.