Excel Process Still Running

D

DeveloperSQL

I have an Windows Forms application that uses the Excel application to create
a spreadsheet, saves it and then quits the excel application - all
programmatically. The problem is that the Excel.exe process remains running
in the background until the windows forms application is exited. I've tried
the following:

//FIRST TRY:

/**Do Garbage Collection as suggested as best practice at:
* http://msdn2.microsoft.com/en-us/library/aa679807(office.11).aspx
*/
excelWorkBooks = null;
mainbook = null;
sheet = null;
someRange = null;
/**Quit the application.*/
excelApplication.Quit();
excelApplication = null;
/**Collect Garbage (Twice).*/
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

//SECOND TRY

private void releaseCOMObject(object COMObject){
try {
if(COMObject != null)

while(System.Runtime.InteropServices.Marshal.ReleaseComObject(COMObject)>0);
}
finally {
COMObject = null;
}
}

//THIRD TRY:
private void releaseCOMObject(object COMObject){
try {
if(COMObject != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(COMObject);
}
finally {
COMObject = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

The methods are invoked after the application is done using the COM objects.

How can I close the Excel.exe process?
 
D

DeveloperSQL

This is a common problem that I've not seen a clear solution for. Any
examples or web links would be helpful.
 
N

NickHK

Most cases of Excel (or other server) failing to close are due to remaining
references.

You need to make sure that EVERY object referenced goes through your
excelApplication object.
Do not use any unqualified objects, as you will then be creating references
to Excel that you cannot set to nothing.

NickHK
 
D

DeveloperSQL

Is there a way to get a debug dump of the active COM objects related to
Excel? I've released all of the COM Objects in my application and Excel.exe
still continues to run.
 
S

Steve Yandl

Below the line is a script I wrote to evaluate certain issues with running
processes. It may give you some hints. Just paste the text into Notepad
and save the file with a vbs extension. Double click it or launch using
WScript.exe or CScript.exe while the rogue Excel process is running. It
will create a new text file report and deposit it on your desktop. Each
running process will be listed along with the process ID as well as the
process ID of any parent process. You should be able to scan the list and
determine what the parent process for the Excel process is or if the Excel
process has a child process or two running.

By the way, you can use WMI from a script to kill a process named EXCEL.EXE.
The problem with that approach is that if you have a different instance of
Excel running, it will be shut down as well and a brute force killing of a
process is not equivalent to properly shutting down the application.

__________________________________

Set objShell = CreateObject("Shell.Application")
Set objFolderDsk = objShell.Namespace(&H10&)
strDsk = objFolderDsk.Self.Path
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strDsk & "\ProcessList.txt",True)
objFile.WriteLine Now & vbCrLf & vbCrLf

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcess
With objProcess
objFile.WriteLine .Name
objFile.WriteLine " PID: " & .ProcessID
objFile.WriteLine " ParentProcessID: " & .ParentProcessID
objFile.WriteLine " " & .CommandLine
objFile.WriteLine ""
objFile.WriteLine ""
End With
Next

objFile.Close
Set colStartups = Nothing
Set objWMI = Nothing
Set objFolderDsk = Nothing
Set objShell = Nothing
Set objFile = Nothing
Set objFSO = Nothing

___________________________________

Steve Yandl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top