Results 1 to 3 of 3

Thread: C# problem. Process Excel.exe not killed

  1. #1

    C# problem. Process Excel.exe not killed

    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]
    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");
    }
    }
    [/CODE]

    Any other way???

  2. #2
    are you just trying to get the excel app to die? I wrote something like this in VB for AutoCad...


    Code:
    Dim d() as System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName( "acad" )
    
    ' kill it! 
    for each curProcess as System.Diagnostics.Process in d
        curProcess.Kill( )
    Next
    Using the process.Kill( ) command might do the trick?

  3. #3
    1,627
    posts
    hugeExplosions = true;
    Need to make sure you call oXL.Quit() when you are finished with it but remember that if you throw an exception you will be skipping the 'quit'

    So put the quit in your 'finally' statement

    Also double check that the excel app isn't waiting for user input in a dialog - if the app is invisible and waiting for user input it will never quit until you end the process.

    Try making oXL.visible = true while you test.
    Last edited by Charleh; October 15th, 2008 at 07:23 AM.
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012