.NET ServicedComponents and STA Apartment
A major issue with running all COM components using STA threads is that the thread which calls CoInitializeEx must be the only thread executing method invocations on that COM instance. Now assume a scenario in which the STA thread is busy performing other activities. In those scenarios you will receive an exception similar to the following and the way around that is to somehow ensure that the message loop on the STA thread is being pumped frequently. In other words anything causing a delay in this process may be an issue and you may receive an exception similar to the following:
“ContextSwitchDeadlock was detected
The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.”
Here is an example of the problem (ComInstance is a ServicedComponent):
Thread t = new Thread(
delegate(object a)
{
ComInstance o = new ComInstance();
Thread tt = new Thread(
delegate(object q)
{
// A deadlock will be detected here
o.ComMethodCall();
});
tt.Start();
while (true)
{
// keep the thread alive
// must pump the message loop
// ...
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
Console.ReadLine();
Comments
Anonymous
August 05, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/08/05/net-servicedcomponents-and-sta-apartment/Anonymous
December 12, 2007
Hi, I have a question regarding STA/MTA in .net I need to build a COM object in .net whose threading model is required to be STA only (not Both,..). is it possible? thanks in advanceAnonymous
December 13, 2007
The comment has been removedAnonymous
December 14, 2007
The comment has been removedAnonymous
August 09, 2008
Mister, if I use Backgroundworker control and in Dowork event I use COM, how I can set STA Apartment ?? Thanks.Anonymous
July 24, 2011
Hi, Regularly I don’t make comments on blogs, but I’ve to point out that it article really forced me to try and do so. Really fabulous blog post. Thanks James