How To Kill WinWOrd Process In C#

When You open MS Word Document pro-grammatically using 'Microsoft.Office.Interop.Word', you are creating 'winword' process. So after dealing with opened MS Word document  pro-grammatically though you close it using 'close' method, the 'winword' process is not terminating, it is still running on your OS. So you have to terminate it in the end of the program otherwise, it creates so many 'winword' processes for each time you open MS Word document and you may face to memory problems later. 

You can use below code to kill 'Winword' process.

foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
            {
                if (!p.HasExited)
                {
                    p.Kill();
                }
            }

Comments

  1. I had good results doing something like this. I like your method too, makes sure it closes.

    while (System.Runtime.InteropServices.Marshal.ReleaseComObject(doc) != 0) { }
    GC.Collect();
    GC.WaitForPendingFinalizers();

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add standard and custom Metadata to PDF using iTextSharp

How to set and get Metadata to image using BitmapMetadata class in C#

How to generate direct line token to start a new conversation between your own client application and bot framework in Java