Tim_Heldberg
asked on
Strange Problem getting Excel Process To Quit`
Hello,
I am working on an application that converts a set of .CSV files to a .XLS workbook. It does this by opening the first csv with Excel, and then opening each additional .CSV and moving it into the existing workbook. The problem that I am having is that after the conversion is done, and all Excel objects are released, the Excel process stays running. Below are some sections of my code.
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook;
Excel.Workbooks workbooks;
Excel.Workbooks tempWorkbooks;
workbooks = (Excel.Workbooks)excelApp. Workbooks;
tempWorkbooks = (Excel.Workbooks)excelApp. Workbooks;
// Open the text file in Excel.
workbooks._OpenText(source Path, Excel.XlPlatform.xlWindows , 1,
Excel.XlTextParsingType.xl Delimited, Excel.XlTextQualifier.xlTe xtQualifie rDoubleQuo te,
false, true, false, false, false, false, missingParameter, missingParameter,
missingParameter, missingParameter, missingParameter);
workbook = excelApp.Workbooks[1];
for (int i = 2; i <= fileCount; i++)
{
tempWorkbooks._OpenText(so urcePath + fileBase + i.ToString() + CSVExtension, Excel.XlPlatform.xlWindows , 1, Excel.XlTextParsingType.xl Delimited, Excel.XlTe xtQualifie r.xlTextQu alifierDou bleQuote,
false, true, false, false, false, false, missingParameter, missingParameter,missingPa rameter, missingParameter,missingPa rameter);
//move the opened sheet to the main WB
((Excel.Worksheet)excelApp .Workbooks [2].Worksh eets[1]).M ove(Type.M issing, excelApp.Workbooks[1].Work sheets[exc elApp.Work books[1].W orksheets. Count]);
}
I have read several posts and have tried every method for releasing the COM objects. What is really strange is that everything works fine and the process is killed if I have the above for loop commented out. This makes it seem like it is a problem in one of 2 the function calls under the for loop, but it will fail regardless of whether the code in the for loop is executed. For example, if there is only one .csv file, the for loop will not execute, but the process will stay running. In the same scenario with the loop commented out, the process dies.
?????
Thanks,
Tim
I am working on an application that converts a set of .CSV files to a .XLS workbook. It does this by opening the first csv with Excel, and then opening each additional .CSV and moving it into the existing workbook. The problem that I am having is that after the conversion is done, and all Excel objects are released, the Excel process stays running. Below are some sections of my code.
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook;
Excel.Workbooks workbooks;
Excel.Workbooks tempWorkbooks;
workbooks = (Excel.Workbooks)excelApp.
tempWorkbooks = (Excel.Workbooks)excelApp.
// Open the text file in Excel.
workbooks._OpenText(source
Excel.XlTextParsingType.xl
false, true, false, false, false, false, missingParameter, missingParameter,
missingParameter, missingParameter, missingParameter);
workbook = excelApp.Workbooks[1];
for (int i = 2; i <= fileCount; i++)
{
tempWorkbooks._OpenText(so
false, true, false, false, false, false, missingParameter, missingParameter,missingPa
//move the opened sheet to the main WB
((Excel.Worksheet)excelApp
}
I have read several posts and have tried every method for releasing the COM objects. What is really strange is that everything works fine and the process is killed if I have the above for loop commented out. This makes it seem like it is a problem in one of 2 the function calls under the for loop, but it will fail regardless of whether the code in the for loop is executed. For example, if there is only one .csv file, the for loop will not execute, but the process will stay running. In the same scenario with the loop commented out, the process dies.
?????
Thanks,
Tim
Oops, almost forgot:
// Close the application
excelApp.Quit();
// Tell COM to release resources.
System.Runtime.InteropServ ices.Marsh al.Release ComObject( excelApp);
System.Runtime.InteropServ ices.Marsh al.Release ComObject( workbooks) ;
System.Runtime.InteropServ ices.Marsh al.Release ComObject( tempWorkbo oks);
// Force garbage collection
GC.Collect();
Bob
// Close the application
excelApp.Quit();
// Tell COM to release resources.
System.Runtime.InteropServ
System.Runtime.InteropServ
System.Runtime.InteropServ
// Force garbage collection
GC.Collect();
Bob
ASKER
I actually did try that. Here is my COM release code
workbook.Close(false, missingParameter, missingParameter);
excelApp.Quit();
workbooks.Close();
while (System.Runtime.InteropSer vices.Mars hal.Releas eComObject (workbook) > 0) { }
while(System.Runtime.Inter opServices .Marshal.R eleaseComO bject(work books)>0){ }
while(System.Runtime.Inter opServices .Marshal.R eleaseComO bject(temp Workbooks) >0){}
while (System.Runtime.InteropSer vices.Mars hal.Releas eComObject (excelApp) > 0) { }
GC.Collect();
workbook.Close(false, missingParameter, missingParameter);
excelApp.Quit();
workbooks.Close();
while (System.Runtime.InteropSer
while(System.Runtime.Inter
while(System.Runtime.Inter
while (System.Runtime.InteropSer
GC.Collect();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I was able to figure it out, although it really doesnt seem like much of a fix. I moved that for loop in question to a seperate function and it worked perfectly. So strange. Any idea why that would make a difference?
Which function? The class destructor?
Bob
Bob
System.Runtime.InteropServ
System.Runtime.InteropServ
System.Runtime.InteropServ
// Force garbage collection
GC.Collect();
Bob