Solved

Close Instance of Excel as a process in Task Manager

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

19 Experts available now in Live!

Get 1:1 Help Now