【发布时间】:2015-09-25 06:15:52
【问题描述】:
我遇到了 EXCEL 应用程序在任务管理器中保持打开状态的问题,即使在我通过代码清理(或认为我正在清理)之后也是如此。谁能指出我在下面的代码的 sn-p 中缺少什么:
private void btnBrowseSquid_Click(object sender, EventArgs e)
//starting up and defining the Excel references
Excel.Workbook squidBook = null;
Excel.Application excelApp = null;
Excel.Worksheet squidSheet = null;
string squidFileName = txtSquid.Text;
//Reading the information from the "Filenames" tab of the SQUID file into the Windows Form
excelApp = new Excel.Application();
excelApp.Visible = true;
squidBook = excelApp.Workbooks.Open(squidFileName);
squidSheet = squidBook.Sheets["Filenames"];
//do stuff here with the excel file
//close the open Excel App
squidBook.Close();
excelApp.Quit();
Marshal.FinalReleaseComObject(squidSheet);
Marshal.FinalReleaseComObject(squidBook);
}
【问题讨论】:
-
你可能有一个旧的进程,因为你的代码没有到达最后的失败尝试......
-
手动内存管理永远不起作用,有一个 WorkSheets 接口引用在您的代码中完全不可见。并且不可避免地你忘记了释放它。只需停止编写这样的代码,没有意义,垃圾收集器永远不会出错。 Read this.