# C# problem. Process Excel.exe not killed

**URL:** <https://forum.kirupa.com/t/c-problem-process-excel-exe-not-killed/272614>\
**Category:** programming\
**Created:** [October 7, 2008, 1:57am UTC](https://forum.kirupa.com/t/c-problem-process-excel-exe-not-killed/272614 "2008-10-07T01:57:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![raul15791](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@raul15791](https://forum.kirupa.com/u/raul15791)\
**Post date:** [October 7, 2008, 1:57am UTC](https://forum.kirupa.com/t/c-problem-process-excel-exe-not-killed/272614/1 "2008-10-07T01:57:46Z")

</div>

Hi,

I’m new to C#. I’m writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the process queue whenever the program quit. Anyway to terminate it automatically when the application exit? I try to use Marshal.ReleaseCOMObject and Garbage Collection. None of these works. Here’s my coding:

[CODE=csharp]  
[SIZE=2][COLOR=#2b91af]SaveFileDialog[/COLOR][/SIZE][SIZE=2] s\_Lvl\_Max = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af]SaveFileDialog[/COLOR][/SIZE]SIZE=2;  
s\_Lvl\_Max.Filter = [/SIZE][SIZE=2][COLOR=#a31515]“xls files (_.xls)|_.xls|All files (_._)|_._”[/COLOR][/SIZE][SIZE=2];  
s\_Lvl\_Max.FilterIndex = 1;  
s\_Lvl\_Max.FileName = [/SIZE][SIZE=2][COLOR=#a31515]“filenamekk”[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]"\_"[/COLOR][/SIZE][SIZE=2] + comboBox\_baseTP.Text;  
s\_Lvl\_Max.OverwritePrompt = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];  
[/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (s\_Lvl\_Max.ShowDialog() == [/SIZE][SIZE=2][COLOR=#2b91af]DialogResult[/COLOR][/SIZE][SIZE=2].OK)  
{  
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]Application[/COLOR][/SIZE][SIZE=2] oXL;  
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]\_Workbook[/COLOR][/SIZE][SIZE=2] oWB;  
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]\_Worksheet[/COLOR][/SIZE][SIZE=2] oSheet;  
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]try  
[/COLOR][/SIZE][SIZE=2]{  
[/SIZE][SIZE=2][COLOR=#008000]//Start Excel and get Application object.  
[/COLOR][/SIZE][SIZE=2]oXL = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] Excel.[/SIZE][SIZE=2][COLOR=#2b91af]Application[/COLOR][/SIZE]SIZE=2;  
oXL.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2];  
oXL.DisplayAlerts = [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2];  
[/SIZE][SIZE=2][COLOR=#008000]//Get a new workbook.  
[/COLOR][/SIZE][SIZE=2]oWB = (Excel.[/SIZE][SIZE=2][COLOR=#2b91af]\_Workbook[/COLOR][/SIZE][SIZE=2])(oXL.Workbooks.Add([/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value));  
oSheet = (Excel.[/SIZE][SIZE=2][COLOR=#2b91af]\_Worksheet[/COLOR][/SIZE][SIZE=2])oWB.Worksheets[1];  
[/SIZE][SIZE=2][COLOR=#008000]//Add table headers going cell by cell.  
[/COLOR][/SIZE][SIZE=2]oSheet.Cells[2, 1] = [/SIZE][SIZE=2][COLOR=#a31515]"Level "[/COLOR][/SIZE][SIZE=2];  
oSheet.Cells[4, 1] = [/SIZE][SIZE=2][COLOR=#a31515]“Test”[/COLOR][/SIZE][SIZE=2];  
oSheet.Cells[4, 2] = [/SIZE][SIZE=2][COLOR=#a31515]“Frequency”[/COLOR][/SIZE][SIZE=2];  
oSheet.Cells[4, 3] = [/SIZE][SIZE=2][COLOR=#a31515]“Spec”[/COLOR][/SIZE][SIZE=2];  
oSheet.Cells[4, 4] = [/SIZE][SIZE=2][COLOR=#a31515]“Actual”[/COLOR][/SIZE][SIZE=2];  
oSheet.Cells[4, 5] = [/SIZE][SIZE=2][COLOR=#a31515]“Guard”[/COLOR][/SIZE][SIZE=2];  
[/SIZE][SIZE=2][COLOR=#008000]//Format A1:D1 as bold, vertical alignment = center.  
[/COLOR][/SIZE][SIZE=2]oSheet.get\_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).Font.Bold = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];  
oSheet.get\_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).VerticalAlignment = Excel.[/SIZE][SIZE=2][COLOR=#2b91af]XlVAlign[/COLOR][/SIZE][SIZE=2].xlVAlignCenter;  
[/SIZE][SIZE=2][COLOR=#008000]// Create an array to multiple values at once.  
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i = 5;  
[/SIZE][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]DataRow[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] dtable2.Rows)  
{  
oSheet.Cells[i, 1] = dr[0];  
oSheet.Cells[i, 2] = dr[1];  
oSheet.Cells[i, 3] = dr[2];  
oSheet.Cells[i, 4] = dr[3];  
oSheet.Cells[i, 5] = dr[4];  
i++;  
}  
i–;  
[/SIZE][SIZE=2][COLOR=#008000]//AutoFit columns A:D.  
[/COLOR][/SIZE][SIZE=2]oSheet.get\_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).EntireColumn.AutoFit();

oWB.SaveAs(s\_Lvl\_Max.FileName, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value,  
[/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, Excel.[/SIZE][SIZE=2][COLOR=#2b91af]XlSaveAsAccessMode[/COLOR][/SIZE][SIZE=2].xlExclusive, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value,  
[/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value);  
[/SIZE][SIZE=2][COLOR=#008000]// Need all following code to clean up and extingush all references!!!  
[/COLOR][/SIZE][SIZE=2]oWB.Close([/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]);  
oXL.Workbooks.Close();  
oXL.Quit();  
[/SIZE][SIZE=2][COLOR=#008000][//System.Runtime.InteropServices.Marshal.ReleaseComObject](https://System.Runtime.InteropServices.Marshal.ReleaseComObject)(oRng);  
[/COLOR][/SIZE][SIZE=2]System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oXL);  
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oSheet);  
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oWB);  
oSheet = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];  
oWB = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];  
oXL = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];  
[/SIZE][SIZE=2][COLOR=#2b91af]GC[/COLOR][/SIZE][SIZE=2].Collect(); [/SIZE][SIZE=2][COLOR=#008000]// force final cleanup!  
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][SIZE=2].Show(s\_Lvl\_Max.FileName + [/SIZE][SIZE=2][COLOR=#a31515]" has been successfully saved. :)"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“File Saved”[/COLOR][/SIZE][SIZE=2]);  
}  
[/SIZE][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]Exception[/COLOR][/SIZE][SIZE=2] theException)  
{  
[/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2] errorMessage;  
errorMessage = [/SIZE][SIZE=2][COLOR=#a31515]"Error: “[/COLOR][/SIZE][SIZE=2];  
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, theException.Message);  
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, [/SIZE][SIZE=2][COLOR=#a31515]” Line: "[/COLOR][/SIZE][SIZE=2]);  
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, theException.Source);  
[/SIZE][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine(errorMessage, [/SIZE][SIZE=2][COLOR=#a31515]“Error”[/COLOR][/SIZE][SIZE=2]);  
}  
}  
[/SIZE]

```auto

 
Any other way???
```
