0

This is the code, I have seen in several other places to close instances of the objects with Marshal.FinalReleaseComObject(...)

This method is called everytime a successful message is sent.

    public static Excel.Workbook MyBook = null;
    public static Excel.Application MyApp = null;
    public static Excel.Worksheet MySheet = null;

    private static void SuccessLog(string communicationMethod, string portNumber, int messageDelay)
    {
        Console.WriteLine(Environment.NewLine);
        DateTime startTime = DateTime.Now;
        string trimDate = Convert.ToString(startTime.ToShortDateString()).Replace('/', '_');
        string folder = @"C:\Temp\";
        string fileName = Convert.ToString(trimDate);
        string message = "Message Sent At: " + Convert.ToString(startTime) + ", via " + communicationMethod + ", at port # " + portNumber + "." + Environment.NewLine;
        string fullPath = folder + fileName;

        MyApp = new Excel.Application();
        MyApp.Visible = false;
        var workbooks = MyApp.Workbooks;
        if (!File.Exists(fullPath + ".xlsx"))
        {
            MyBook = workbooks.Add();
            MySheet = MyBook.Sheets[1]; // Explicit cast is not required here
            int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
            MySheet.Cells[lastRow, 1] = "asdfsa";
            MySheet.Cells[lastRow, 2] = "asdfsa";
            MySheet.Cells[lastRow, 3] = "asdfsa";
            MySheet.Cells[lastRow, 4] = "asdfsa";
            
            MyBook.SaveAs(fullPath);
            MyBook.Close(0);
        }
        else
        { 
            MyBook = workbooks.Open(fullPath + ".xlsx");
            MySheet = MyBook.Sheets[1]; // Explicit cast is not required here

            int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
            lastRow ++;
            MySheet.Cells[lastRow, 1] = "exists?";
            MySheet.Cells[lastRow, 2] = "exists?";
            MySheet.Cells[lastRow, 3] = "exists?";
            MySheet.Cells[lastRow, 4] = "exists?";
            MyBook.Save();
            MyBook.Close(0);
        }

Here is where I am trying to close all of the Objects that I am creating. I think I may possibly be missing some of the objects.

        MyApp.Quit();
        Marshal.FinalReleaseComObject(MySheet);
        Marshal.FinalReleaseComObject(MyBook);
        Marshal.FinalReleaseComObject(MyApp);
        Marshal.FinalReleaseComObject(workbooks);
        Console.WriteLine("Log has been successfully updated or added at: " + fullPath);
        Console.WriteLine("Next message will be sent in: " + MessageDelay + " seconds. (Specified in Config.)");
   }
  • Where are you calling the code to release the com objects in relation to the code above it? Typically you would wrap all that code into a `try/catch/finally` statement. It is unclear “where” the last code snippet is called. – JohnG Feb 16 '22 at 18:54
  • Sorry that it is unclear this is all within a method. I have not wrapped this in a try catch yet. After this method there is another method that is run from where it is called from. – king2lc Feb 16 '22 at 19:08
  • And what makes you believe that the com objects are not getting released or the Excel app is not closing? In my small tests, your code appears to successfully close and release the com objects it creates. – JohnG Feb 16 '22 at 19:21
  • That is a good question, I may be misunderstanding what the com objects are. When I start and close the console app it leaves one open in task manager at all times. I can see it create and close excel instances during its loop while watching task manager but the one it creates when running does not get closed. – king2lc Feb 16 '22 at 19:30
  • “When” are you “checking” that the Excel app is still in the task manager. The manager may not release the app until the code exits. In other words, if you put a break-point on the last line of code… `Console.WriteLine("Next message will be sent in: " + MessageDelay + " seconds. (Specified in Config.)");` … then the Excel app will still be visible in the task manager even though the code has released it. When the app exits, then the Excel app should get removed from the task manager. – JohnG Feb 16 '22 at 19:46
  • You state that… _”I can see it create and close excel instances during its loop while watching task manager ….”_ … what loop? If your code creates multiple instances of the Excel app, then those instances may not get released until the app exits. It is not clear “when” you are “checking” the task manager. You need to keep in mind that you are at the mercy of the garbage collector and it may not necessarily immediately “release” the app when the code releases the object. However, it “should” get released if the app exits. – JohnG Feb 16 '22 at 19:46
  • You're are hitting the RCW issue: https://stackoverflow.com/a/47852419/495455 – Jeremy Thompson Feb 18 '22 at 02:13

1 Answers1

-2

Various versions of excel have, for about 2 weeks now, not closed properly, and keep a lock to accdb files even though (using ADO) the connection is closed and the object is set to nothing. Since Office versions vary, I have to assume that Windows (10) has managed to break yet another thing. Until about 2 weeks ago none of this was an issue. Yay Microsoft!