RE: Wrapping Word.Application object with an IDisposable class?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks for your very informative post.

Unfortunately, the whole point is to ensure the COM object is disposed if
there is an unhandled exception in my program, so I can't guarantee calling
..Quit from the creating thread.

Multi-threading isn't my strong suit - I usually write fairly high-level apps.

That KB article looks very useful, perhaps there is a better way using
ReleaseComObject().

-DCS

"Jialiang Ge [MSFT]" wrote:

Hello,

From your post, my understanding on this issue is: you want to know why the
Word process is not closed in Dispose method when the application is tested
in Visual Studio debug mode. If I'm off base, please feel free to let me
know.

I did a test with your WordFactory class. In order to see why the Word
process is not closed in Dispose, I removed the "On Error Resume Next" and
"On Error GoTo 0" around "mobjWordApp.Quit()", then I got the exception
thrown by Visual Studio debugger at the sentence:
*COM object that has been separated from its underlying RCW cannot be
used.*
This problem is most likely caused that your COM objects are called on
different threads. The creation of the COM object is on one thread (the
main thread in this issue), and the clean on another thread (the .NET
Garbage Collection thread in this context). By default we use STA apartment
model so if you are using COM objects in different threads, this may cause
issues like interop exceptions "object disconnected from its RCW". It means
that we need to keep track of the thread which created those COM objects.
If that thread ends, the COM object will also go away even if the RCW that
.NET is using is still alive because the GC has not cleaned it yet (Dispose
has not been called). In your question, you said that the problem only
occurs in VS IDE, but based on my test, sometimes the exception also occurs
when I run the application directly. In order to resolve the problem, I
still suggest that you call mobjWordApp.Quit in the thread where
wobjWordApp is created because Office COM object is designed as STA. Please
also call System.Runtime.InteropServices.Marshal.ReleaseComObject to ensure
all the reference count to the COM object is decreased. (See the kb article
Office application does not quit after automation from Visual Studio .NET
client http://support.microsoft.com/kb/317109).

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • RE: Oracle Client ORA-03113 error hangs my windows service
    ... To answer your question, Kevin, the service exception handling works ... service does use multiple threads, but they are all handled by the .NET ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: Wrapping Word.Application object with an IDisposable class?
    ... Word process is not closed in Dispose method when the application is tested ... in Visual Studio debug mode. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: making code native in a C++/CLI program
    ... which makes the exception very strange. ... // but can't mix and match in-place copy construction ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vc)
  • RE: Ok to call Application.get_Selection()?
    ... We get an exception that says COMException ... Cannot create a Selection object when this dialog is active. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.office.developer.com.add_ins)
  • RE: Strange LocalDataStoreSlot storage has been freed exception
    ... that this exception is generated in the finalizer of LocalDataStoreSlot. ... However using reflector I don't see such an exception being thrown from the ... Microsoft Online Community Support ... nature are best handled working with a dedicated Microsoft Support Engineer ...
    (microsoft.public.dotnet.framework.clr)