?
Solved

Close Instance of Excel as a process in Task Manager

Posted on 2010-11-17
2
Medium Priority
?
307 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 2000 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

Independent Software Vendors: 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 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