InvalidCOMObjectException:已经从其底层RCW分离的COM对象不能使用? [英] InvalidCOMObjectException: COM object that has been separated from its underlying RCW cannot be used?

查看:1341
本文介绍了InvalidCOMObjectException:已经从其底层RCW分离的COM对象不能使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行测试时抛出此异常:

I'm getting this exception thrown when I run my tests:


测试方法 OuvertureClasseur threw异常:System.Runtime.InteropServices.InvalidComObjectException:已经从其底层RCW分离的COM对象不能使用...

Test method OuvertureClasseur threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used...


$ b $

though each of my test passes when ran individually.

以下是我的测试代码:

<TestClass()> _
Public Class FabriqueExcelTests
    Private Shared _xl As ApplicationClass

    <ClassInitialize()> _
    Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
        _xl = New ApplicationClass()
        _xl.Visible = False
        _xl.ScreenUpdating = False
        _xl.DisplayAlerts = False
    End Sub

    <ClassCleanup()> _
    Public Shared Sub MyClassCleanup()
        _xl.Quit()
        _xl = Nothing
    End Sub

    <TestMethod()> _
    Public Sub ConstructeurParDefaut()
        Dim gestionnaire As GestionnaireExcel = New GestionnaireExcel()
        Assert.IsNotNull(gestionnaire)
        Assert.IsInstanceOfType(gestionnaire, GetType(GestionnaireExcel))
    End Sub

    <TestMethod()> 
    Public Sub CreationDUnFichierExcel()
        Dim fichier As String = "C:\Temp\CreationFichierExcel.xls"

        Using xl As GestionnaireExcel = New GestionnaireExcel()
            Dim classeur As Workbook = xl.CreerClasseur(fichier)
            classeur.Close()
            classeur = Nothing
        End Using

        Assert.IsTrue(File.Exists(fichier))
        File.Delete(fichier)
    End Sub

    <TestMethod()> _
    Public Sub OuvertureClasseur()
        Dim fichier As String = "C:\Temp\OuvertureClasseur.xls"

        _xl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
        _xl.ActiveWorkbook.SaveAs(fichier)
        _xl.ActiveWorkbook.Saved = True
        _xl.Workbooks.Close()

        Using xl As GestionnaireExcel = New GestionnaireExcel()
            Dim classeur As Workbook = xl.OuvrirClasseur(fichier)
            Assert.AreEqual(fichier, classeur.FullName)
            classeur.Close()
            classeur = Nothing
        End Using

        File.Delete(fichier)
    End Sub

End Class

我的 GestionnaireExcel class:

Public Class GestionnaireExcel
    Implements IDisposable

    Private _cultureOriginale As CultureInfo
    Private _disposedValue As Boolean = False        '' To detect redundant calls
    Private _excel As ApplicationClass

    Public Sub New()
        InitialiserExcel()
    End Sub

    '' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me._disposedValue Then
            If disposing Then
                '' TODO: free other state (managed objects).
                If (_excel IsNot Nothing) Then
                    _excel.Quit()
                    _excel = Nothing
                    System.Threading.Thread.CurrentThread.CurrentCulture = _cultureOriginale
                End If
            End If

            '' TODO: free your own state (unmanaged objects).
            '' TODO: set large fields to null.
        End If

        Me._disposedValue = True
    End Sub

    '' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        '' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Public Function CreerClasseur(ByVal classeur As String) As Workbook
        _excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
        _excel.ActiveWorkbook.SaveAs(classeur)
        Return _excel.ActiveWorkbook
    End Function

    Private Sub InitialiserExcel()
        _cultureOriginale = System.Threading.Thread.CurrentThread.CurrentCulture
        System.Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")

        _excel = New ApplicationClass()
        _excel.DisplayAlerts = False
        _excel.ScreenUpdating = False
        _excel.Visible = False
    End Sub

    Public Function OuvrirClasseur(ByVal fichier As String) As Workbook
        Dim classeur As Workbook = _excel.Workbooks.Open(fichier _
                                                          , False, False _
                                                          , Type.Missing, Type.Missing,              Type.Missing, Type.Missing, Type.Missing, Type.Missing _
                                                          , False _
                                                          , Type.Missing, Type.Missing _
                                                          , False _
                                                          , Type.Missing, Type.Missing)
        Return classeur
    End Function
End Class




strong>相关问题

虽然我已阅读并了解这些问题和答案,什么原因导致我的 OuvertureClasseur()测试失败时,我一起运行所有的测试,它运行正常,当我单独运行。

Though I have read and understand these questions and answers, I don't seem to find what causes my OuvertureClasseur() test to fail when I run all my tests together, and it works just fine when I run it individually.



  1. / 3953864 / wmi-exception-com-object-that-has-been-been-separated-from-its-underlying-rcw-can not> WMI异常:已从其底层RCW分离的COM对象无法使用 a>


  2. / 1492879 / com-object-that-has-been-been-been-been-separated-from-its-underlying-rcw-can-not-be-used-why>与其底层RCW分离的COM对象不能使用 - 为什么


  1. COM object that has been separated from its underlying RCW cannot be used.
  2. WMI Exception: "COM object that has been separated from its underlying RCW cannot be used"
  3. COM object that has been separated from its underlying RCW cannot be used.
  4. COM object that has been separated from its underlying RCW can not be used - why does it happen?
  5. WPF Thread: "COM object that has been separated from its underlying RCW cannot be used."

我已经跑了几个小时了,任何帮助将很高兴。

I have been running around for few hours now, and any help will gladly be appreciated.

提前感谢! =)

推荐答案

Excel作为另一个进程执行。这可能是发生在classeur.Close()是异步的,并且其中一些发生在GestionnaireExcel实例被处理后。

Excel's executing as another process. It's possible that what's happening in classeur.Close() is asynchronous, and that some of it is taking place after the GestionnaireExcel instance is disposed.

尝试注释掉使用块,看看会发生什么。或者,如果Excel COM对象暴露一个属性,说明应用程序是否仍然活着,尝试放入一个循环来监视,而不是离开你的使用块,直到你确定它已经消失。

Try commenting out the using block and see what happens. Or if the Excel COM object exposes a property saying whether the app is still alive, try putting in a loop to monitor that and not leave your using block until you're sure it's gone.

这只是一个猜测,但我看到还没有其他人回答,所以我会折腾它。

This is just a guess, but I see nobody else has answered yet, so I'll toss it out there.

这篇关于InvalidCOMObjectException:已经从其底层RCW分离的COM对象不能使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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