13

I'm writing an application that needed a MSWord document parser.

I'm using Microsoft.Office.Interop.Word.Document to extract the texts from the documents, but even if i use doc.Close() the document, from taskManager i can see that winword.exe are not killed, and after parsing a couple dozens documents it eats up some much resources.

is close() the wrong method?

please help me and point me to the right direction on how to terminate these processes properly. =)

~~~update~~~

Thanks for all the help. I use the app.quit() and also ran a loop that checks for the process and problem solved! =)

Krispy
  • 133
  • 1
  • 1
  • 5
  • This is a side effect of the way the garbage collector works. You probably don't generate enough garbage to ever trigger a collection. In this very specific case, it is okay to call GC.Collect + GC.WaitForPendingFinalizers. – Hans Passant Aug 15 '11 at 20:18
  • 1
    @Hans Passant Not really anything to do with the Garbage Collector. He's not calling the right method to clean up. – MGZero Aug 15 '11 at 20:35

5 Answers5

20

Are you calling Application.Quit , additionally since you're doing Interop it may be worthwhile to release the RCW wrapper.

So basically something like:

yourWordAppObject.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(yourWordAppObject);

Note some folks use: ReleaseComObject but there are some potential pitfalls

Ta01
  • 31,040
  • 13
  • 70
  • 99
3

You must quit the application instance using app.quit(). Document.close() will just close the document. I also suggest setting app.visible = true when you're done processing so your user can close it themselves if all else fails.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • 2
    Application.Quit doesn't actually terminate the process, nor does the user closing the window. Word cannot exit until all the automation interfaces are released. – Hans Passant Aug 15 '11 at 20:44
  • @Hans Passant Oh shit, you're right. How do we go about doing this? Especially in cases where you want the document to be visible to the user and to be closed by them as well. – MGZero Aug 16 '11 at 14:42
3

After performing the app.Quit(), you must do app = null; From my experiences, this will prevent leftover processes from hanging around. Just be sure to do the app.Quit() and app = null in your exception handler as well.

k.schroeder31
  • 801
  • 2
  • 8
  • 15
2

If you want to end the process you need to call Quit on the Application object - see http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.applicationclass.quit%28v=office.14%29.aspx

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

I am thinking close just handles the document open inside word. Remember you can have more than 1 word document open with 1 application. You may want to try either a dispose method, or look at the word objects quit/exit methods (can't remember its been a while).

Matt
  • 1,441
  • 1
  • 15
  • 29