Proper Interop Cleaning

I have a method for checking spelling in my home application. As a new programmer, this was compiled by several sources and changed until it worked for me.

As I grow and study, I come across things that make me go, hmm. Like this SO post, How to properly clean Excel interaction objects in C # , this indicates that Interop is cleaned properly.

I noticed that he repeatedly mentions the use of Marshal.FinalReleaseComObject()or Marshal.ReleaseComObject().

My question is, based on the code below, do I need this too? Thanks

        public string CheckSpelling(string text)
    {
        Word.Application app = new Word.Application();
        object nullobj = Missing.Value;
        object template = Missing.Value;
        object newTemplate = Missing.Value;
        object documentType = Missing.Value;
        object visible = false;
        object optional = Missing.Value;
        object savechanges = false;
        app.ShowMe();

        Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);

        doc.Words.First.InsertBefore(text);
        Word.ProofreadingErrors errors = doc.SpellingErrors;

        var ecount = errors.Count;
        doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional);
        object first = 0;
        object last = doc.Characters.Count - 1;
        var results = doc.Range(ref first, ref last).Text;
        doc.Close(ref savechanges, ref nullobj, ref nullobj);
        app.Quit(ref savechanges, ref nullobj, ref nullobj);

        return results;
    }
+3
source share
2

. Marshal.ReleaseComObject COM .NET.

+2

. Word._Document doc = app.Documents.Add(...); _Documents, . :

Word._Documents docs = app.Documents;
Word._Document doc = docs.Add(...);
// release docs and doc after use

. , COM- , , , , doc.Words.First.InsertBefore(text);.

+1

Source: https://habr.com/ru/post/1728347/


All Articles