Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi ,

I am comparing two word files using word Compare API. I am using Visual Studio and C# for the development.
The Word Compare is working fine as expected, but every time I am invoking word compare, it is creating new Instance of WINWORD.exe. and that instance is still there in Task Manager, even when I close the word documents.

How to prevent multiple instances of WINWORD.EXE getting created.

My code:
C#
private void compare_files(string file1, string file2)
{
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;
    wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
    object wordTrue = (object)true;
    object wordFalse = (object)false;
    object fileToOpen = file1;
    object missing = Type.Missing;
    try
    {
        Microsoft.Office.Interop.Word.Document doc1 = new Microsoft.Office.Interop.Word.Document();
        doc1 = wordApp.Documents.Open(ref fileToOpen,
           ref missing, ref wordFalse, ref wordFalse, ref missing,
           ref missing, ref missing, ref missing, ref missing,
           ref missing, ref missing, ref wordTrue, ref missing,
           ref missing, ref missing, ref missing);
        doc1.Compare(file2, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    }
    catch
    {
    }
}
Posted
Updated 6-Feb-12 9:58am
v2

1 solution

You should probably think about using a Singleton Pattern, to make sure that only one instance of Microsoft.Office.Interop.Word.Application object is ever created.

A singleton Pattern is a tried an tested design pattern wherein the consumer of a class make sure that only one instance of a class is ever created.

Check out a basic implementation of it below:

C#
public class WordInstanceSingleton
{
   private static Microsoft.Office.Interop.Word.Application m_wordInstance;

   private WordInstanceSingleton() {}

   public static Microsoft.Office.Interop.Word.Application Instance
   {
      get 
      {
         if (m_wordInstance == null)
         {
            m_wordInstance = new Microsoft.Office.Interop.Word.Application();
         }
         return m_wordInstance ;
      }
   }
}


In the place where you create an instance of the Microsoft.Office.Interop.Word.Application class replace it with the following code:

C#
Microsoft.Office.Interop.Word.Application wordApp = WordInstanceSingleton.Instance;


There are many variations of the SingletonPattern, and if your compare code exists within a multi-threaded environment, then you will have to make changes in the Singleton Pattern. Read more about it here.Though always keep in mind that the WordApplication is not designed to work in a multi-threaded environment, and if you make your application work with the same instance on multiple documents, it might not work predictably and will be unstable.

Also, remember that you will have to make sure that the Microsoft.Office.Interop.Word.Application should be disposed using either "Quit()" or Close()method, once the comparison is done or when you close the application.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900