Solved

Close Instance of Excel as a process in Task Manager

Posted on 2010-11-17
2
301 Views
Last Modified: 2012-05-10
Hi

I have a method that creates an Excel file. After running the method I close the Excel instance but it still shows up as a process in the task manager. When I quit my application, this instance disappears.

I can run the Excel method multpile times and it will always leave one instance running

I know it is bad coding to leave an instance running so I would like to close it.

Attached is my code

 
// Create an Excel object and add workbook...
Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true); 

// Do Excel Processing Here

// Dispose of Excel
excel.Quit();           
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
GC.WaitForPendingFinalizers();

Open in new window

0
Comment
Question by:elmbrook
2 Comments
 
LVL 10

Accepted Solution

by:
wls3 earned 500 total points
ID: 34161392
Have you tried removing the workbook object as well?  

It appears this is not uncommon.  A few creative answers were provided in this link:

http://stackoverflow.com/questions/350454/c-interop-excel-process-not-exiting-after-adding-new-worksheet-to-existing-file

The P/Invoke approach to find the handle might work well.  There are equivalent .NET approaches that may be cleaner in terms of interop with the System.Diagnostics.Process object.

http://www.codegod.de/webappcodegod/Kill-process-by-name-with--NET-AID185.aspx
0
 

Author Comment

by:elmbrook
ID: 34161454
Thanks wls3
I did figure it out in the end but will allocate you the points.
Below is my solution

// Open Instance of Excel
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

GC.Collect();// clean up any other excel guys hangin' around...
oXL = new Excel.Application();
oXL.Visible = false;

//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;

// Do Processing in here

// Tidy up Processing
oWB.Close(null, null, null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oSheet = null;
oWB = null;
oXL = null;
GC.Collect();
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 47
Coding C# in Linux 8 68
VB.NET (2008) - Refactoring Question 2 21
Hey!! 5 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question