A COM Interop Question
-
Look at the following code
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit();
The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel? -
Look at the following code
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit();
The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel? -
Look at the following code
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit();
The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?When using COM via .Net you need to make sure you release all instances, and point to null when done. check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109[^] It explains the problem and the resolution. Notice that you might also need to release workbooks, depends on your code.
-
Look at the following code
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit();
The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?Here is a code piece from Visio Code Librarian:
/// This method releases all references to a COM object. When
/// Visual Studio .NET calls a COM object from managed code, it
/// automatically creates a Runtime Callable Wrapper (RCW). The RCW
/// marshals calls between the .NET application and the COM object. The
/// RCW keeps a reference count on the COM object. Calling
/// ReleaseComObject when you are finished using an object will cause
/// the reference count of the RCW to be decremented.
/// param name="runtimeObject": The runtime callable wrapper whose
/// underlying COM object will be released.
private void NullAndRelease(object runtimeObject)
{
try
{if (runtimeObject != null) { // The RCW's reference count gets incremented each time the // COM pointer is passed from unmanaged to managed code. // Call ReleaseComObject in a loop until it returns 0 to be // sure that the underlying COM object gets released. int referenceCount = System.Runtime.InteropServices. Marshal.ReleaseComObject(runtimeObject); while (0 < referenceCount) { referenceCount = System.Runtime.InteropServices. Marshal.ReleaseComObject(runtimeObject); } }
}
finally
{
runtimeObject = null;
}
}I think you can also use this one for Excel too. Regards,
Always keep the Murphy Rules in mind!
modified on Thursday, August 21, 2008 6:15 AM
-
Here is a code piece from Visio Code Librarian:
/// This method releases all references to a COM object. When
/// Visual Studio .NET calls a COM object from managed code, it
/// automatically creates a Runtime Callable Wrapper (RCW). The RCW
/// marshals calls between the .NET application and the COM object. The
/// RCW keeps a reference count on the COM object. Calling
/// ReleaseComObject when you are finished using an object will cause
/// the reference count of the RCW to be decremented.
/// param name="runtimeObject": The runtime callable wrapper whose
/// underlying COM object will be released.
private void NullAndRelease(object runtimeObject)
{
try
{if (runtimeObject != null) { // The RCW's reference count gets incremented each time the // COM pointer is passed from unmanaged to managed code. // Call ReleaseComObject in a loop until it returns 0 to be // sure that the underlying COM object gets released. int referenceCount = System.Runtime.InteropServices. Marshal.ReleaseComObject(runtimeObject); while (0 < referenceCount) { referenceCount = System.Runtime.InteropServices. Marshal.ReleaseComObject(runtimeObject); } }
}
finally
{
runtimeObject = null;
}
}I think you can also use this one for Excel too. Regards,
Always keep the Murphy Rules in mind!
modified on Thursday, August 21, 2008 6:15 AM
-
When using COM via .Net you need to make sure you release all instances, and point to null when done. check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109[^] It explains the problem and the resolution. Notice that you might also need to release workbooks, depends on your code.
Thank you for your reply.But I stiil face a problem:
private void NullAndRelease(object runtimeObject) { if (runtimeObject == null) return; try { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(runtimeObject); } finally { runtimeObject = null; } } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); object NullParam = System.Reflection.Missing.Value; Workbook Book = ExcelApp.Workbooks.Open(FileName, 0, true, NullParam, NullParam, NullParam, true, NullParam, NullParam, false, false, NullParam, false, true, NullParam); Worksheet Sheet = Book.Worksheets[1] as Worksheet; Range UsedRange = Sheet.UsedRange; Range Cell = null; int RowCount = UsedRange.Rows.Count; for (int i = 2; i <= RowCount; i++) { Cell = UsedRange.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 2] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 3] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 4] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 5] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 6] as Microsoft.Office.Interop.Excel.Range; NullAndRelease(Cell); } NullAndRelease(Cell); NullAndRelease(UsedRange); NullAndRelease(Sheet); if (Book != null) Book.Close(false, NullParam, false); NullAndRelease(Book); ExcelApp.Quit(); NullAndRelease(ExcelApp); GC.Collect(); GC.WaitForPendingFinalizers(); }
These code works well.But when I add the following code in the for loop:Cell = UsedRange.Cells[i, 7] as Microsoft.Office.Interop.Excel.Range;
The server does not shut down. It seems that I can only read not more than 6 cells.That's why?