Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Strange Problem getting Excel Process To Quit`

Posted on 2006-07-12
6
554 Views
Last Modified: 2008-01-09
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(sourcePath, Excel.XlPlatform.xlWindows, 1,
      Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
      false, true, false, false, false, false, missingParameter, missingParameter,
      missingParameter, missingParameter, missingParameter);


workbook = excelApp.Workbooks[1];

for (int i = 2; i <= fileCount; i++)
{
   tempWorkbooks._OpenText(sourcePath + fileBase + i.ToString() + CSVExtension, Excel.XlPlatform.xlWindows, 1,                  Excel.XlTextParsingType.xlDelimited,Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
      false, true, false, false, false, false, missingParameter, missingParameter,missingParameter,  missingParameter,missingParameter);
                              
                                    
//move the opened sheet to the main WB
((Excel.Worksheet)excelApp.Workbooks[2].Worksheets[1]).Move(Type.Missing, excelApp.Workbooks[1].Worksheets[excelApp.Workbooks[1].Worksheets.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
0
Comment
Question by:Tim_Heldberg
  • 4
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17093242
// Tell COM to release resources.
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempWorkbooks);

// Force garbage collection
GC.Collect();

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17093257
Oops, almost forgot:

// Close the application
excelApp.Quit();

// Tell COM to release resources.
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempWorkbooks);

// Force garbage collection
GC.Collect();

Bob
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 17093402
I actually did try that.  Here is my COM release code

workbook.Close(false, missingParameter, missingParameter);

excelApp.Quit();
workbooks.Close();

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook) > 0) { }
while(System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks)>0){}
while(System.Runtime.InteropServices.Marshal.ReleaseComObject(tempWorkbooks)>0){}
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp) > 0) { }
                        
GC.Collect();
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17093703
Yeah, didn't notice the remark in the question body.  Sounds like some resource is being still not being released, but I can't see what that would be.  It might be something internal to the _OpenText call.

Bob
0
 
LVL 4

Author Comment

by:Tim_Heldberg
ID: 17093760
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17093815
Which function?  The class destructor?

Bob
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question