Excel process not terminating after use in C# console app

Al Harlow 20 Reputation points
2024-02-07T19:36:54.0966667+00:00

I am creating spreadsheets in a C# console app using Excel. When I try to terminate the Excel process and get the window handle, I get an 'InvalidComObjectException' the second time through. The first Excel image in memory is deleted but thereafter Excel is not terminated. How can I fix this issue using .NET 4.8 and Windows 11?

Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-02-08T09:35:52.36+00:00

    Hi @Al Harlow , Welcome to Microsoft Q&A,

    Maybe you want to see a use case of creating excel using c#:

    using System;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace ExcelCreationExample
    {
         class Program
         {
             static void Main(string[] args)
             {
                 //Create Excel application object
                 Excel.Application excelApp = new Excel.Application();
    
                 //Create a new workbook
                 Excel.Workbook workbook = excelApp.Workbooks.Add();
    
                 // Get the first worksheet
                 Excel.Worksheet worksheet = workbook.Worksheets[1];
    
                 //Write sample data in cell A1
                 worksheet.Cells[1, 1] = "Hello";
                 worksheet.Cells[1, 2] = "World";
    
                 //Save workbook
                 workbook.SaveAs("example.xlsx");
    
                 // Close the workbook
                 workbook.Close();
    
                 // Close the Excel application
                 excelApp.Quit();
    
                 // Release the COM object
                 ReleaseObject(worksheet);
                 ReleaseObject(workbook);
                 ReleaseObject(excelApp);
             }
    
             // Release the COM object
             static void ReleaseObject(object obj)
             {
                 try
                 {
                     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                     obj = null;
                 }
                 catch (Exception ex)
                 {
                     obj = null;
                     Console.WriteLine("Exception Occured while releasing object " + ex.ToString());
                 }
                 finally
                 {
                     GC.Collect();
                 }
             }
         }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2024-02-07T20:45:45.8133333+00:00

    You didn't post your code so I'm going to guess you are not properly cleaning up the references to the COM object. In addition to closing any Excel window you might have opened you also need to call Quit on the application. You need to ensure this happens even in the case of exceptions otherwise you'll leave processes hanging around.

    Also note that even after you clean up a COM object the underlying COM runtime will potentially leave the process running for a while in case you try to start it again. It used to be 10 minutes IIRC but I don't know what the default is now. However if your app closes then it should close the other process as well provided you cleaned up all the resources properly.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.