L'exemple de code suivant a très bien fonctionné dans Excel 2007, mais lorsque j'installais Excel 2010 (32 bits), le processus excel.exe restait ouvert sauf si j'ajoutais GC.Collect(). Ma question simple est-ce que je fais quelque chose de mal? Il me semble que je libère tout ce que j'utilise.Références de l'objet com Excel 2010 non publiées
public override void Update()
{
StatusBox.AddStatus("Opening File " + ImportPath);
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(ImportPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
Range rng = ws.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
int LastRow = rng.Row;
StatusBox.AddStatus(LastRow.ToString() + " Rows Found in File");
StatusBox.AddStatus("Closing File " + ImportPath);
System.Runtime.InteropServices.Marshal.ReleaseComObject(rng);
rng = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
wb.Close(true, ImportPath, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
wb = null;
GC.Collect();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
Je fais un peu de COM interop avec Excel et MapPoint. Je ne pense pas avoir jamais besoin d'utiliser ReleaseComObject. Effacer toutes les références nulles et appeler les méthodes de fermeture/fermeture appropriées a suffi. Quelqu'un peut-il confirmer/développer le besoin éventuel de ReleaseComObject() dans ces situations de type Office? – winwaed
J'ai ajouté ReleaseComObject quand j'avais des problèmes avec la suspension des références Excel la première fois que j'ai écrit le code pour Excel 2003. J'ai effectivement lu que var = null; pourrait être inutile. –