3
  1. 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.

  2. 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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Likurg
  • 2,742
  • 17
  • 22
  • Not sure if this covers your issues completely, but you should definetely know http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c-sharp since Word might have similar issues. – TheBlastOne Apr 25 '12 at 13:22
  • Its better to see why 3 instances are starting, instead of closing them all..Imagine when a user is using a word app and your program closing his instance? – Flowerking Apr 25 '12 at 13:23
  • @TheBlastOne Look at my code, i use it – Likurg Apr 25 '12 at 13:23
  • @Flowerking i agree with you, but i cant understand it too, so tell me if you know it. – Likurg Apr 25 '12 at 13:24
  • @Likurg Wouldn't you then have incorporated the GC.Collect() and GC.WaitForPendingFinalizers calls? I.e. I am referring to Mike Rosenblum's answer, and the link contained therein. – TheBlastOne Apr 25 '12 at 13:29
  • @TheBlastOne I use it but 1 process didn't die – Likurg Apr 25 '12 at 13:34
  • Btw thank you all for so fast answered and advised – Likurg Apr 25 '12 at 13:35
  • 1
    Searching for "COM object that has been separated from its underlying RCW" at SO shows lots of answered questions with different backgrounds, try browsing those? – TheBlastOne Apr 25 '12 at 14:03

2 Answers2

4

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.

Community
  • 1
  • 1
dkackman
  • 15,179
  • 13
  • 69
  • 123
-2

//To find out the WINWORD.exe process and set RealTime Priority to WINWORD.exe //This is used to quick process

Process[] proces = Process.GetProcessesByName("WINWORD");


foreach (Process proc in proces)

{

   proc.PriorityClass = ProcessPriorityClass.RealTime;
}