Why is my Excel office object leaving behind EXCEL.exe processes?
本问题已经有最佳答案,请猛点这里访问。
0
我有一个应用程序使用了Office Interop库。
此库是从以iis7为宿主的ASP.NET应用程序调用的。
相关的代码位是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /// <summary> /// Provides excel functions. /// </summary> public class ExcelStuff : IDisposable { #region Excel Components // The following are pre-allocated Excel objects that must be explicitly disposed of. private Excel.Application xApp; private Excel.Workbooks xWorkbooks; private Excel.Workbook xWorkbook; private Excel.Sheets xWorksheets; private Excel.Worksheet xWorksheet; private Excel.ChartObjects xCharts; private Excel.ChartObject xMyChart; private Excel.Chart xGraph; private Excel.SeriesCollection xSeriesColl; private Excel.Series xSeries; #endregion /// <summary> /// Initializes a new instance of the <see cref="ExcelStuff" /> class. /// </summary> public ExcelStuff() { } /// <summary> /// Does some work. /// </summary> public void DoWork() { byte[] data = new byte[0]; worker = new Thread(() => { // Do some work. }); worker.Start(); worker.Join(); worker.Abort(); worker = null; Dispose(); } /// <summary> /// Disposes the current object and cleans up any resources. /// </summary> public void Dispose() { // Cleanup xWorkbook.Close(false); xApp.Quit(); // Manual disposal because of COM xApp = null; xWorkbook = null; xWorksheets = null; xWorksheet = null; xCharts = null; xMyChart = null; xGraph = null; xSeriesColl = null; xSeries = null; GC.Collect(); } } |
您会注意到代码中有很多冗余,这是我为解决这个问题所做的绝望的尝试。所以请不要告诉我打电话给
我想知道的是,当我尽最大努力清理尽可能多的对象时,为什么这段代码会让孤立的
当有人在上述marshal.releasecomobject Comment is to,我开始清理使用Excel对象。这里是我的代码:sample ofP></
1 2 3 4 5 6 7 8 9 | wb.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value); while (System.Runtime.InteropServices.Marshal.ReleaseComObject(wb) != 0) { } excelApp.Quit(); while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp) != 0) { } |
这似乎为我工作。再见!P></
编辑:对不起,我不看这是个DUP。任何与MODS can do this…P></