Select the product you need help with
A console application that is based on a STA may delay the release of COM components and may delay the calls to the Finalize methods of the objects that the garbage collector collectsArticle ID: 828988 - View products that this article applies to. SYMPTOMSIf a console application that is based on a single-threaded apartment (STA) creates and then uses STA Component Object Model (COM) components and the console application does not perform sufficient
operations to pump COM messages, such as calling the Monitor.Enter method, the Thread.Join method, and others, the following symptoms may occur. Also, if the console application performs operations that that run for a long time and that do not
pump messages, such as calling the Console.ReadLine method, the following symptoms may occur:
CAUSETo deliver a call to a STA COM component correctly, including a call to release the component, the thread where the component was created must be pumping messages. When the managed code no longer references a COM component, the finalizer thread must
call the Release method of the component. However, if the STA thread where
the component was created is not pumping messages, the call blocks the application until the
thread starts pumping messages again. Some applications create a lot
of COM components and objects that can be finalized. In these applications, if the
finalizer thread spends lot of time blocked while it waits for the STA
thread to pump messages, the finalizer thread may not be able to complete its activity. Both the list of COM
components to release and the list of managed objects to finalize
may grow indefinitely. If this issue occurs, the memory amount that the STA application process uses may increase over time. Also, any thread that calls the GC.WaitForPendingFinalizers method may block indefinitely. The file and console input/output (IO) operations of the operating system, such as ReadFile and WriteFile, do not pump messages. Because Console.ReadLine relies on the ReadFile API of the operating system, Console.ReadLine does not pump messages either. Therefore, any thread that calls Console.ReadLine does not pump messages during the call. RESOLUTIONTo resolve this issue, use MTA threads instead of STA threads to create and to communicate with
COM components in console applications. You can set the main thread of the application to a MTA. To do this, apply the System.MTAThread
attribute to the main method, as follows: REFERENCESFor more information about Microsoft .NET Framework remoting, visit the following Microsoft Web site: http://msdn2.microsoft.com/en-us/library/1912sb9w(vs.71).aspx For
more information about garbage collection in the .NET Framework, visit the
following Microsoft Web sites:
(http://msdn2.microsoft.com/en-us/library/1912sb9w(vs.71).aspx)
http://msdn.microsoft.com/msdnmag/issues/1100/gci/
(http://msdn.microsoft.com/msdnmag/issues/1100/gci/)
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/
(http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/)
For additional information about blocking calls from a STA component, click the following article number to view the article in the Microsoft Knowledge Base: 291837
(http://support.microsoft.com/kb/291837/
)
INFO: Do not make blocking calls from a STA component
PropertiesArticle ID: 828988 - Last Review: February 9, 2004 - Revision: 1.4
|


Back to the top








