我已经阅读了几篇有关从.NET进行交互时正确处理COM对象的文章/帖子。通常我没有问题..但是我关闭下面的程序后仍然收到孤立的COM对象。我不知道它是否与我打开的插件有关。
Application app = new Application();
Workbooks wrks = app.Workbooks;
Workbook wrk = null;
Workbook wrkAddin = null;
Sheets shts = null;
Range rng = null;
try
{
app.ScreenUpdating = true;
app.Visible = true;
app.DisplayAlerts = false; // stop any dialog boxes appearing.. such as save or debug when loading bloomberg addin.
wrk = wrks.Open(Filename: MasterPriceFiles.Properties.Resources.strETFDataFullPath, ReadOnly: false,Editable: true, IgnoreReadOnlyRecommended: true);
Thread.Sleep(500);
wrkAddin = wrks.Open(Filename: MasterPriceFiles.Properties.Resources.strBloombergAddinPath); // have to explicitly open the bloomberg addin
app.Visible = true;
Thread.Sleep(50000);
shts = wrk.Worksheets;
foreach (Worksheet sht in shts)
{
rng = sht.UsedRange;
rng.Copy();
rng.PasteSpecial(XlPasteType.xlPasteValues);
Marshal.ReleaseComObject(rng);
Marshal.ReleaseComObject(sht);
}
app.Visible = false;
app.DisplayAlerts = false;
Thread.Sleep(500);
wrk.SaveAs(Filename: MasterPriceFiles.Properties.Resources.strETFDataMacroFreeFullPath, ConflictResolution: XlSaveConflictResolution.xlLocalSessionChanges, AccessMode: XlSaveAsAccessMode.xlNoChange);
Console.WriteLine("Successfully saved ETF price data sheet at . " + DateTime.Now.ToString());
}
catch(Exception ex)
{
Console.WriteLine("Error refreshing ETF price date. " + ex.Message);
}
finally
{
if (rng != null) Marshal.FinalReleaseComObject(rng);
if (shts != null) Marshal.FinalReleaseComObject(shts);
wrk.Close(SaveChanges: false);
if (wrk != null) Marshal.FinalReleaseComObject(wrk);
wrkAddin.Close(SaveChanges: false);
if (wrkAddin != null) Marshal.FinalReleaseComObject(wrkAddin);
if (wrks != null) Marshal.FinalReleaseComObject(wrks);
app.Quit();
if (app != null) Marshal.FinalReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}