GC for Interop - .NET ADDIn

T

tingletanglebob

Hi,

I am currently writing a COM-AddIn for Word, Excel, Visio and PP. Because of
an incorporation of Office into an EDRMS System I have to make changes via
their API which uses System._ComObject objects. Certain new Toolbar objects
in word etc. must be refreshed after a document was saved. Due to the missing
"Document_After_Save" I am using a Timer (System.Timers.Timer) which then
refreshes the new Toolbar objects. This works fine so far, but Word, Excel
etc. finishes not properly (there is still a Task running in the TaskManager,
even if there is no application window visible anymore).

I was able to pinpoint it down to the Timer. Each Time the Timer uses a
System._ComObject (e.g. the Office 2003 Tollbar objects) it leaves a task in
the Taskmanager. If I am using purely .Net during the Timer Event it works
fine and the Application shuts down properly. I tried things like forcing the
Garbage collection etc. but nothing helps.

What can be the problem for the .Net Timer using Com - Objects?

I appreciate any suggestions - thanks in advance
 
H

Helmut Obertanner

Hello,

whenever you use a COM Object from .net you should explicitly release the
reference to this Object when it isn't no longer needed.

This is done by Marshal.ReleaseComObject( yourComObject );
Also as I know sometimes word stays in memory, depending on how you used it.
For example if you use word as Email editor for Outlook.

--
Helmut Obertanner
Technical Consultant
Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich

.... and IT works!
 
T

tingletanglebob

Thanks Helmut,

Iam releasing now the whole Office Application (Word etc.) and it works fine
now as far as I can see it. I just have a question left. The MSDN Library
says that the released object is zero after releasing it. My result is -1
..Does this ring any bells? I just think to use the release object in a while
statement for all remaining tasks (as suggested in the MSDN Library) until
the result is 0. But mine is -1. What does it mean?

Thanks so for - it was very helpful!
 
H

Helmut Obertanner

Hello tingletanglebob,

I'm not sure what the -1 result means
but try to release like this

if ( YourComObject != null) Marshal.ReleaseComObject( YourYomObject );

After all do a
GC.WaitForPendingFinalizers();
GC.Collect();


Maybe that helps.

regards
--
Helmut Obertanner
Technical Consultant
Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich

.... and IT works!
 
A

Alex

tingletanglebob said:
Thanks Helmut,

Iam releasing now the whole Office Application (Word etc.) and it works fine
now as far as I can see it.

Does releasing the Word application release all the objects beneath it?
 
T

tingletanglebob

yes, it works quite successfully and stops everything what runs in the
context of the Office App.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top