Microsoft.Interop对象不会退出或“释放". [英] Microsoft.Interop object won't quit or "release"

查看:44
本文介绍了Microsoft.Interop对象不会退出或“释放".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行此代码时,对象都不会关闭.我仍然在任务管理器中运行excel.exe.即使我将对象设置为null,也仍然没有任何效果.我什至尝试使用对象.Quit()方法.

Everytime I run this code, the object won't close. I still have an excel.exe running in the task manager. Even if I set the objects = null, still nothing. I've even tried using the objects .Quit() method.

我在做什么错了?

    private bool ValidateQM()
    {
        //setup the objects
        Excel.Application oXL = null;
        Excel.Workbook oWB = null;
        Excel.Worksheet oSheet = null;
        int hWnd = 0;

        try
        {

            //Start Excel and get Application object.
            oXL = new Excel.Application();
            hWnd = oXL.Application.Hwnd;
            oXL.Visible = false;

            //Open the workbook.
            oWB = oXL.Workbooks.Open(workingForm, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",true, false, 0, true, false, false);

            //Get the Worksheet
            oSheet = oWB.Worksheets[1];

            //Check the date values

            string mydatetime = oSheet.Cells[5, 33].Text.ToString() + " " + oSheet.Cells[7, 33].Text.ToString();
            string dateofscore = oSheet.Cells[3, 12].Text.ToString();

            DateTime.Parse(mydatetime); //make my string a real boy
            DateTime.Parse(dateofscore);

            // Cleanup 
            GC.Collect();
            GC.WaitForPendingFinalizers();

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oSheet);

            //oWB.Close();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oWB);

            //oXL.Quit();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oXL);

            return true;
        }

推荐答案

调用GC.Collect()时,oXL和oWB对象仍在作用域内,因此有一个激活的引用.结果,GC.Collect()不会碰到它们.如果要确保将它们垃圾回收,请将它们设置为null,以便在调用GC.Collect()时没有激活的引用.另外,您可能要在活动工作簿上调用Close(),在Excel应用程序上调用Quit().您还可以使用大括号{}将所有内容放在其自己的范围内,以使它们一起超出范围.

When you call GC.Collect(), the oXL and oWB objects are still in scope so there's a reference active to them. As a result, GC.Collect() won't touch them. If you want to ensure they are garbage collected, set them to null so there's no references active when GC.Collect() is called. Also, you may want to call Close() on the active workbook and Quit() on the Excel application. You could also put everything in its own scope with braces { } so that it all went out of scope together.

        // Cleanup 
        oWB.Close(false);
        oWB = null;
        oXL.Quit();
        oXL = null;
        hWnd = null;
#if (DEBUG_SPEED_UP_GC)
        GC.Collect();
        GC.WaitForPendingFinalizers();
#endif

编辑:请注意,手动调用垃圾回收是一个坏主意.我假设您只是出于调试目的这样做,以便在发生GC时加快速度,因此我添加了#if.

Edit: Note that manually calling garbage collection is a bad idea. I am assuming you are only doing this for debugging purposes to speed up when the GC occurs, so I added the #if.

这篇关于Microsoft.Interop对象不会退出或“释放".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆