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

  • SEARCH
  • Home
  • 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 8139559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:51:56+00:00 2026-06-06T11:51:56+00:00

Possible Duplicate: How to properly clean up Excel interop objects in C# I have

  • 0

Possible Duplicate:
How to properly clean up Excel interop objects in C#

I have an application that uses Office Interop libraries for C#.

This library is called from an ASP.NET application hosted with IIS7.

The relevant code bits are:

/// <summary>
/// Provides excel functions.
/// </summary>
public class ExcelStuff : IDisposable
{
    #region Excel Components

    // The following are pre-allocated Excel objects that must be explicitly disposed of.

    private Excel.Application xApp;
    private Excel.Workbooks xWorkbooks;
    private Excel.Workbook xWorkbook;
    private Excel.Sheets xWorksheets;
    private Excel.Worksheet xWorksheet;
    private Excel.ChartObjects xCharts;
    private Excel.ChartObject xMyChart;
    private Excel.Chart xGraph;
    private Excel.SeriesCollection xSeriesColl;
    private Excel.Series xSeries;

    #endregion

    /// <summary>
    /// Initializes a new instance of the <see cref="ExcelStuff" /> class.
    /// </summary>
    public ExcelStuff()
    {
    }

    /// <summary>
    /// Does some work.
    /// </summary>
    public void DoWork()
    {
        byte[] data = new byte[0];

        worker = new Thread(() =>
        {
            // Do some work.
        });

        worker.Start();
        worker.Join();

        worker.Abort();
        worker = null;

        Dispose();
    }

    /// <summary>
    /// Disposes the current object and cleans up any resources.
    /// </summary>
    public void Dispose()
    {
        // Cleanup
        xWorkbook.Close(false);
        xApp.Quit();

        // Manual disposal because of COM
        xApp = null;
        xWorkbook = null;
        xWorksheets = null;
        xWorksheet = null;
        xCharts = null;
        xMyChart = null;
        xGraph = null;
        xSeriesColl = null;
        xSeries = null;

        GC.Collect();
    }
}

You will notice a lot of redundancies in the code, which are my desperate attempt to solve this problem. So please don’t tell me that it’s inefficient to call GC.Collect() or that calling Dispose() from within the class isn’t good – I know these things already.

What I would like to know is, why this code keeps orphaned EXCEL.exe processes running on the server when I’m doing my best to clean up as many objects as possible.

  • 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-06-06T11:51:58+00:00Added an answer on June 6, 2026 at 11:51 am

    As someone mentioned in a comment, Marshal.ReleaseComObject is what I use to clean up excel objects. Here is a sample of my code:

            wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
    
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(wb) != 0)
            { }
    
            excelApp.Quit();
    
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp) != 0)
            { }
    

    This seems to work for me. Good luck!

    EDIT: Sorry, didn’t see this was a dup. Mods can do whatever with this…

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

Sidebar

Related Questions

Possible Duplicate: How to properly clean up Excel interop objects in C# I've this
Possible Duplicate: How to properly clean up Excel interop objects in C# I've read
Possible Duplicate: My regex is not working properly Suppose I have long text. From
Possible Duplicate: Best Way to Sprite Images? I have the following image that I
Possible Duplicate: running sql statement from excel vba Here's the excerpt that's causing the
Possible Duplicate: In java, in this program it suddenly stops running properly at a
Possible Duplicate: How do you create a Perl module? I have the script that
Possible Duplicate: How to “properly” create a custom object in JavaScript? Sorry if this
Possible Duplicate: Pick random property from a Javascript object suppose I have a javascript
Possible Duplicate: Making a generic property I'm not quite sure how to do that,

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.