Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensuring WINWORD not overhanging after usage

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
We've got some code that makes use of MS Word in the sense that it makes use of code similar to that which follows :

Word.Application Word_App = new Word.Application();
Word.Document Word_doc = new Word.Document();
Word.Documents Docs = Word_App.Documents;
Word._Document Doc1 = (Word._Document)Word_doc;

.. more code to process the document ...
.. and print it ....
Doc1.PrintOut(ref oTrue, ref oFalse, ref oPrintRange,
ref oMissing, ref oMissing, ref oMissing,
ref oPrintItems, ref oPrintCopies, ref oPrintPages,
ref oPrintPageType, ref oFalse,
ref oTrue, ref oMissing, ref oFalse, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);

// Close the document and Word
Word_App.Visible = false;
// Ensure the document and Word are discarded
try
{
Doc1.Close(ref oFalse, ref oMissing, ref oMissing);
}
catch {}
try
{
Docs.Close(ref oFalse, ref oMissing, ref oMissing);
}
catch {}
try
{
Word_doc.Close(ref oFalse, ref oMissing, ref oMissing);
}
catch {}
try
{
Doc1 = null;
}
catch {}
try
{
Docs = null;
}
catch {}
try
{
Word_doc = null;
}
catch {}
try
{
Word_App = null;
}
catch {}

Try as we might we still find that we have an instance of 'WINWORD.exe' running when we use the 'Task Manager'.
Clearly this is an overhang from what we're doing and we need to prevent it.

Can anyone shed any light on what clean-up step we're overlooking ?
Thanks in advance
Steve
 
I think you should add :
...
Word_App.Quit()
if (Word_App!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject( Word_App )
if (Word_Doc!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_Doc)
-obislavu-

 
After calling close, you should call the .Dispose method on your COM objects. What's happening is the garbage collector hasn't run yet, so the objects are still in memory. After calling .Dispose, go ahead and set them to null if you like.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi,
Chip is right, in my above example you have to add:

Word_App.Quit()
if (Word_App!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject( Word_App )
if (Word_Doc!=null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(Word_Doc)
Word_App = null;
Word_Doc = null;
GC.Collect();


-obislavu-
e.g.
 
I wouldn't actually call GC.Collect, but rather call .Dispose on the objects that need it. Forcing a GC cycle to run could take longer than anticipated, and give a bad user experience.

The GC algorithm is quite good, and having people call .Collect all over the place is a Band-Aid (Sticking Plaster to the UK folks), and doesn't make up for good coding practices.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Again, Chip is right but GC is working very well depending on the application (example, small applications).
If the processing of the Excel application is part of the thread then calling GC is okay and there is no impact to the other applicaton threads.

StavenK could confirm.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top