How close Winword.exe in my application

c#, ms-word

Solution

You are doing this in a finalizer (different than a destructor). Finalizers are non-deterministic, meaning by the time they run class members may have already been finalized and thus would no longer be valid.

I would implement the Dispose pattern and explicitly control the lifetime of your word COM objects. This answer has a lot of good links that may help you.

Problem

When i started my app and opened word file i see 3 process winword.exe in taskmanager. After i called 'close' function 1 process winword.exe closed. When i called worddoc.close() or wordapp.quit() in destructor, i got exception "COM object that has been separated from its underlying RCW cannot be used." ``` public class WordHelper { private object nullobj = System.Reflection.Missing.Value; public string context = ""; Microsoft.Office.Interop.Word.Document doc = new Document(); Microsoft.Office.Interop.Word.Application wordApp = new Application(); public WordHelper(string FileName) { //Open word file } //somefunction fo work with file public void CloseWord() { doc.Close(); wordApp.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp); } ~WordHelper() { //i got exception doc.Close(); wordApp.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp); } } ``` How i call my class ``` WordHelper wddoc = new WordHelper("C:\\Test Word\\Test.docx"); wddoc.CloseWord(); //this line i use and can close 1 process not 3 //One process close after i close application ``` At the end i want close all winword.exe which was opened by my application, and i want to close them in destructor. At the end, i need to close all 'winword.exe' which was opened by my application, and i need to close them in destructor.

Original source

Related problems