Solved

Close Instance of Excel as a process in Task Manager

Posted on 2010-11-17
2
300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now