Office application does not quit after automation from Visual Studio .NET client(从VS.NET客户端自动化后,Office应用程序不退出)

Symptoms(现象)
         When you automate a Microsoft Office application from Microsoft Visual Basic .NET or Microsoft Visual C# .NET,  
         the Office application does not quit when you call the Quit method. 
               从Microsoft Visual Basic .NET或Microsoft Visual C# .NET自动化Microsoft Office application后,即使调用Quit
         法,Office应用程序还是不会退出.

Cause(原因)
         When Visual Studio .NET calls a COM object from managed code, it automatically creates a Runtime Callable
         Wrapper (RCW). The RCW marshals calls between the .NET application and the COM object. The RCW keeps a 
         reference count on the COM object. Therefore, if all references have not been released on the RCW, the COM
         object does not quit.
               从托管代码调用COM对象后,它自动创建一个运行时可调用包装(RCW).RCW在.NET应用程序与COM对象之间封送调用,并且保 
         持COM对象的引用计数.因此,如果没有释放RCW上的所有引用,COM对象是不会退出的.

Resolution(解决办法)
          To make sure that the Office application quits, make sure that your automation code meets the following
          criteria:
               为了保证Office应用程序退出,必须确保你的自动化代码满足如下标准:
          1.Declare each object as a new variable. For example, change the following line of code
             将每一个对象声明为一个新的变量.例如,改变如下的代码行
                oBook = oExcel.Workbooks.Add();
             to the following: 成为:
                Excel.WorkbooksoBooks ;
                oBooks = oExcel.Workbooks ;
                oBook = oBooks.Add() ;
           2.Use System.Runtime.InteropServices.Marshal.ReleaseComObject when you have finished using an
              object.This decrements the reference count of the RCW.
              你使用完一个对象后,调用System.Runtime.InteropServices.Marshal.ReleaseComObject.这会减少RCW的引用.

           3.To release the reference to the variable, set the variable equal to Nothing or Null.
              为了释放变量引用,设置变量为Nothing或者Null.
           4.Use the Quit method of the Office application object to tell the server to shut down.
              使用Office应用程序对象的Quit方法告诉服务器关闭Excel.

Troubleshooting(排除异常)
           Note that if you follow the steps, and the server still does not shut down, you can use the GC.Collect() 
           method and the GC.WaitForPendingFinalizers() method after you release the last object. Because the runtime
           performs garbage collection on the RCW, the GC.Collect() method forces the garbage collector to run and
           might release any references that the RCW still has. The GC.Collect() method tries to reclaim the maximum
           memory that is available. Note that this does not guarantee that all memory will be reclaimed. 
           注意:如果你按照这些步骤做了,服务器还是不关闭Excel,你可以在释放完对象后,使用GC.Collect()方法与
           GC.WaitForPendingFinalizers()方法.因为运行时对RCW执行垃圾回收,所以GC.Collect()强迫运行垃圾回收器,并且释放RCW持
           有的任何引用.GC.Collect()总是试图回收尽可能多的内存.必须清楚的是这并不保证回收所有可用内存.

From http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109

For more information:

Microsoft does not recommend this approach:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;257757

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值