C# problem. Process Excel.exe not killed when application quit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raul15791
    New Member
    • Oct 2008
    • 2

    C# problem. Process Excel.exe not killed when application quit

    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 read some previous postings on the same problem and try to use Marshal.Release COMObject and Garbage Collection. None of these works. Here's my coding:

    Code:
    SaveFileDialog s_Lvl_Max = newSaveFileDialog();
    s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
    s_Lvl_Max.FilterIndex = 1;
    s_Lvl_Max.FileName = "filenamekk" + "_" + comboBox_baseTP.Text;
    s_Lvl_Max.OverwritePrompt = true;
    if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
    {
    Excel.Application oXL;
    Excel._Workbook oWB;
    Excel._Worksheet oSheet;
    try
    {
    //Start Excel and get Application object.
    oXL = new Excel.Application();
    oXL.Visible = false;
    oXL.DisplayAlerts = false;
    //Get a new workbook.
    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
    oSheet = (Excel._Worksheet)oWB.Worksheets[1];
    //Add table headers going cell by cell.
    oSheet.Cells[2, 1] = "Level ";
    oSheet.Cells[4, 1] = "Test";
    oSheet.Cells[4, 2] = "Frequency";
    oSheet.Cells[4, 3] = "Spec";
    oSheet.Cells[4, 4] = "Actual";
    oSheet.Cells[4, 5] = "Guard";
    //Format A11 as bold, vertical alignment = center.
    oSheet.get_Range("A1", "E1").Font.Bold = true;
    oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
    // Create an array to multiple values at once.
    int i = 5;
    foreach (DataRow dr in 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--;
    //AutoFit columns A.
    oSheet.get_Range("A1", "E1").EntireColumn.AutoFit();
    
    oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    // Need all following code to clean up and extingush all references!!!
    oWB.Close(null, null, null);
    oXL.Workbooks.Close();
    oXL.Quit();
    //System.Runtime.InteropServices.Marshal.ReleaseComO bject(oRng);
    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(); // force final cleanup!
    MessageBox.Show(s_Lvl_Max.FileName + " has been successfully saved. ", "File Saved");
    }
    catch (Exception theException)
    {
    String errorMessage;
    errorMessage = "Error: ";
    errorMessage = String.Concat(errorMessage, theException.Message);
    errorMessage = String.Concat(errorMessage, " Line: ");
    errorMessage = String.Concat(errorMessage, theException.Source);
    Console.WriteLine(errorMessage, "Error");
    }
    }
    Any other way???
  • l034n
    New Member
    • Oct 2008
    • 15

    #2
    You'll need to kill excel, as it's not quitting, even if you call Quit().

    Code:
    private void KillExcel()
    {
    	Process[] excelProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
    
    	foreach(Process p in excelProcesses)
    	{
    		p.Kill();
    	}
    }
    Well, that will effectively kill all Excel processes, so if you have opened 10 excel instances outside your app, when you call this function all of them will close. If you want to prevent it, you should check for PID's.

    Comment

    Working...