Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:27+00:00 2026-05-23T13:24:27+00:00

I am using COM Excel Application class in C#. I handled the WorkbookBeforeClose .

  • 0

I am using COM Excel Application class in C#. I handled the WorkbookBeforeClose. But now I can’t release the COM objects properly.

Note that I managed to release the COM objects properly before handling this event and when commenting that part of code, my application works properly.

What COM object is not released from memory and how to release it properly?

Edit:
What I’ve done:

public void Init()
{
   ...
   application = new Excel.Application();
   application.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(application_WorkbookBeforeClose);
}
...
void application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel)
    {
        if (WorkbookBeforeClose != null)
        {
            ExcelCloseEventArgs args = new ExcelCloseEventArgs();
            WorkbookBeforeClose(this, args);
            Cancel = args.Cancel;
        }
        else
            Cancel = false;
    }
...
private void closeExcel()
    {
        try
        {
            if (workbook != null)
            {
                workbook.Close(false);
            }
        }
        catch (Exception e) { }
        finally
        {
            if (workbooks != null)
                Marshal.ReleaseComObject(workbooks);
            if (workbook != null)
                Marshal.ReleaseComObject(workbook);
        }

        try
        {
            if (application != null)
            {
                application.WorkbookBeforeClose -= handler;
                application.Quit();
                Marshal.ReleaseComObject(application);
                Marshal.ReleaseComObject(handler);
                process.WaitForExit();
            }
        }
        catch (Exception e) { throw; }
        finally
        {

        }

        workbook = null;
        workbooks = null;
        application = null;
        if (process != null && !process.HasExited)
            process.Kill();
        if (threadCulture != null)
            Thread.CurrentThread.CurrentCulture = threadCulture;
        initialized = false;
    }

Application pauses In line process.WaitForExit().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:24:28+00:00Added an answer on May 23, 2026 at 1:24 pm

    Memory management is automatic in .NET. Explicitly calling Marshal.ReleaseComObject() yourself is a mistake. Not just because you can easily crash the RCW when you do so too early, but especially because the reference counts are often hidden. Indexers and events are the tricky ones. One missing ReleaseComObject call is enough to make it fall back to the garbage collector taking care of it. That the GC takes some time to get around to releasing memory (and reference counts) is a feature, not a bug.

    If you really, really want the COM server to quit on demand instead of letting the garbage collector take care of it then set all the references you have to null, unsubscribe the event handlers and call GC.Collect(). No ReleaseComObject calls required. Check this blog post for insight from the pros.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the core problem: I have a .NET application that is using COM interop
I'm using the Excel.Application COM object from a Python program to open a CSV
I got an application that automates Microsoft Excel (2003) using Office Automation. One of
Using the Excel COM automation interface I can set value in a cell by
I am using some of the functions of .net application in excel through COM.
I'm writing a Excel class using Microsoft.Interropt.Excel DLL. I finish all function but I
Background: I'm using MEF to help compose an Excel 2007 Add-In (VSTO) that can
I'm using the Excel COM interop to insert images (specifically EPS) into a spreadsheet.
I'm writing an Excel Addin using COM Interop from .net. I have a command
What are profound advantages that one get when using COM for developing components over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.