Reputation: 71
An Excel background process remains open after each time I open and read a file with Iterop. After running the below method a few times I end up with a lot of Excel background processes. I realise this has been asked before various times but I have tried all the suggestions (and implemented them in the code below) and nothing worked. Could someone please help.
public List<double> ReadExcelFile()
{
Application excelApp = null;
Workbooks workbooks = null;
Workbook workBook = null;
Worksheet worksheet = null;
Range excelRange = null;
List<double> sheetValues = new List<double>();
try
{
excelApp = new Application();
workbooks = excelApp.Workbooks;
workBook = workbooks.Open(f_inputFilePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
worksheet = workBook.Sheets["Sheet1"];
excelRange = worksheet.UsedRange;
object[,] sheetValuesRaw = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
for (int i = f_firstNumericValueIndex; i <= sheetValuesRaw.GetLength(0); i++)
sheetValues.Add((double)sheetValuesRaw[i, 1]);
}
finally
{
workBook.Close(false, Type.Missing, Type.Missing);
workbooks.Close();
releaseObject(excelRange);
releaseObject(worksheet);
releaseObject(workbooks);
releaseObject(workBook);
excelApp.Quit();
releaseObject(excelApp);
}
return sheetValues;
}
private void releaseObject(object obj)
{
try
{
Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Upvotes: 4
Views: 733
Reputation: 957
I know this question is quite old, but I was running into this issue as well. The garbage collection did not work. I found another solution that seems to work. I added the following to my code and the background processes are removed.
try
{
SetExcelFiles(rdgsFilePath, unreadFilePath);
if (rdgsFilePath != "") ProcessReadingsFile();
if (unreadFilePath != "") ProcessUnreadFile();
}
catch (Exception)
{
Data = new List<BadgeData>();
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
xlApp.Quit();
System.Diagnostics.Process[] PROC = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process PK in PROC)
{
if (PK.MainWindowTitle.Length == 0)
{
PK.Kill();
}
}
Upvotes: -1
Reputation: 115
I had a few problems with the Excel interop stuff myself a few weeks back. I managed to solve the process hang however by wrapping the method which uses the Excel functionality in a try-catch-finally statement.
So it would look something like this:
public List<double> MyResults() {
try {
return ReadExcelFile();
}
finally {
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
And you would call the 'MyResults' method in place of where ever you currently have 'ReadExcelFile' being called.
I found that by wrapping the Excel functionallity inside another try-catch-finally loop outside of the method using the Interop services, it allowed the Excel bits to go out of scope and allow the GC to collect them up.
Upvotes: 2