RE: Wrapping Word.Application object with an IDisposable class?
- From: surturz <surturz@xxxxxxxxxxxxxxxx>
- Date: Wed, 10 Oct 2007 22:21:01 -0700
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.
- Follow-Ups:
- RE: Wrapping Word.Application object with an IDisposable class?
- From: Jialiang Ge [MSFT]
- RE: Wrapping Word.Application object with an IDisposable class?
- References:
- Wrapping Word.Application object with an IDisposable class?
- From: surturz
- RE: Wrapping Word.Application object with an IDisposable class?
- From: Jialiang Ge [MSFT]
- Wrapping Word.Application object with an IDisposable class?
- Prev by Date: Serial port encoding
- Next by Date: RE: Find Mouse Coordinates Within Control
- Previous by thread: RE: Wrapping Word.Application object with an IDisposable class?
- Next by thread: RE: Wrapping Word.Application object with an IDisposable class?
- Index(es):
Relevant Pages
|