Solved

Close Instance of Excel as a process in Task Manager

Posted on 2010-11-17
2
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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