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.)");
   }
7
  • 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. Commented Feb 16, 2022 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. Commented Feb 16, 2022 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. Commented Feb 16, 2022 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. Commented Feb 16, 2022 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. Commented Feb 16, 2022 at 19:46

1 Answer 1

-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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.