I am using C# & VS2013 and VSTO. My code executes to perfection, but the one issue I have is that when I go to Task Manager and look at Details, Excel is still showing as running even after, I feel like I have properly cleaned up.
This is the syntax I use to cleanup and close all references to Excel. Just the references to Excel & VSTO, I left out the meat and gravy that does the leg work or this post would be to long.
Do I have something set-up improperly? Why does Excel still show up in Task Manager?
namespace ReleaseExcel
{
public partial class Form12 : Form12
{
public static Excel.Worksheet newSeet;
public static Excel._Worksheet currentsheet;
public static Excel._Workbook oWB;
public static Excel.Application xlApp;
public static Excel.Workbook xlWorkBook;
public static Excel.Worksheet xlWorkSheet;
public static Excel.Workbook activeworkbook;
public Form1()
{
InitializeComponents();
}
private void DoThis()
{
//Showing Declerations to Excel
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//Do some more stuff here
//Showing a few more Declerations to Excel
currentsheet = (Excel._Worksheet)(xlWorkBook.ActiveSheet);
Excel.Range last = currentsheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
//Do a bit more stuff
ClearExcel();
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) != 0) { }
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(currentsheet) != 0) { }
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) != 0) { }
}
private static void ClearExcel()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
EDIT
I added in GC.Collect()
& GC.WaitForPendingFinalizers()
but now my syntax hangs on
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) != 0) { }
It has been sitting on this line for about 5 minutes now, what causes the freeze?
EDIT # 2
Still no such luck. This is code
namespace ReleaseExcel
{
public partial class Form12 : Form12
{
public static Excel.Worksheet newSeet;
public static Excel._Worksheet currentsheet;
public static Excel._Workbook oWB;
public static Excel.Application xlApp;
public static Excel.Workbook xlWorkBook;
public static Excel.Worksheet xlWorkSheet;
public static Excel.Workbook activeworkbook;
public Form1()
{
InitializeComponents();
}
public void btnPush_Click(object sender, EventArgs e)
{
DoThis();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void DoThis()
{
//Showing Declerations to Excel
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//Do some more stuff here
//Showing a few more Declerations to Excel
currentsheet = (Excel._Worksheet)(xlWorkBook.ActiveSheet);
Excel.Range last = currentsheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
}
}
}