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 7070167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:32:01+00:00 2026-05-28T05:32:01+00:00

My situation is that I’m developing a C# application which is launching an instance

  • 0

My situation is that I’m developing a C# application which is launching an instance of a Microsoft Office Excel Application.
I change some of the Form’s functions so that the instantiated Excel Application is being killed and cleaned up from memory when my Form’s being closed.

What I want to do is to perform the opposite. I’d like my instance of Excel to close my windows Form when it’s being exited.

  • 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-28T05:32:02+00:00Added an answer on May 28, 2026 at 5:32 am

    Ok, I haven’t even tried this so I’m not sure if it will work, but you could try something along these lines:

    Microsoft.Office.Interop.Excel.Application excel = ...;
    
    System.Diagnostics.Process excelProcess = null;
    var processes = System.Diagnostics.Process.GetProcesses();
    
    foreach (var process in processes)
    {
            if (process.Handle.ToInt32() == excel.Hwnd)
                excelProcess = process;
    }
    
    excelProcess.Exited += new EventHandler(excelProcess_Exited);
    

    UPDATE

    Try the following code (its not pretty):

    Microsoft.Office.Interop.Excel.Application xlsApp;
    Process xlsProcess;
    Timer timer;
    
    public Form1()
    {
        xlsApp = new Microsoft.Office.Interop.Excel.Application();
        xlsApp.Visible = true;
    
        foreach (var p in Process.GetProcesses()) //Get the relevant Excel process.
        {
            if (p.MainWindowHandle == new IntPtr(xlsApp.Hwnd))
            {
                xlsProcess = p;
                break;
            }
        }
    
        if (xlsProcess != null)
        {
            timer = new Timer();
            timer.Interval = 500;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }
    }
    
    void timer_Tick(object sender, EventArgs e)
    {
        if (xlsApp != null && !xlsApp.Visible)
        {
            //Clean up to make sure the background Excel process is terminated.
            xlsApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
            xlsApp = null;
        }
    
        if (xlsProcess.HasExited)
        {
            //The event is not fired but the property is eventually set once the process is terminated
            this.timer.Dispose();
            this.Close();
        }
    }
    

    The problem when working with an Excel Application is that even if the user closes it’s main window, the process will keep on running in the background. In order to release it completely you have to call Application.Quit but you should only do that when you detect the user closing the window which is exactly the problem you are trying to solve…you have a circular reference here 😀

    Latching to the relevant System.Diagnostics.Process doesn’t seem to work either, as the Exited event never gets fired (not even if you terminate the background process from the task manager). My guess is that this event is fired only with processes launched through process = Process.Start(...).

    So the only solution I can see is polling if Excel`s main window is visible. Once it is not, that probably means the user closed the main window and the Excel application should be terminated (if there is some scenario where Excel’s main window’s visibility can be false other than when the user wants to terminate the process then this solution is not valid). From there it is pretty straightforward.

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

Sidebar

Related Questions

I have a situation that if my form doesn't validate I need it to
If givin some situation that you can do a loop of a certain event
I have a situation that users access remote MySQL server in C# application. Basically,
An everyday debugging situation for Java developers is that in which an Exception is
I have a situation that I cannot change: one database table (table A) accepts
I have a situation that I'm sure must be fairly common. I have some
There is a situation that I am faced with which seems like it has
There are some situation that I need to convert string to float or some
So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber =
I have a situation that has been partially covered by other answers at SO,

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.