如果我在c#中关闭应用程序,是否有必要释放ComObject?

时间:2018-02-18 16:52:37

标签: c# ms-word garbage-collection marshalling comobject

我有以下代码,我想知道是否需要手动释放ComObject,否则垃圾收集器会为我发布它?

wordApp = new Word.Application();
wordDoc = new Word.Document();
// some stuff

wordDoc.Close(null, null , null);
wordApp.Quit();
if(System.Runtime.InteropServices.Marshal.IsComObject(wordDoc))
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
wordDoc = null;
if (System.Runtime.InteropServices.Marshal.IsComObject(wordApp))
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;

上述代码是否正确或最终我不应该使用Marshal.IsComObject

0 个答案:

没有答案