[英]COM object that has been separated from its underlying RCW cannot be used, only in test debug
I am starting a WPF application from an MSTest (as a STA thread).我正在从 MSTest(作为 STA 线程)启动 WPF 应用程序。 I start the application in the assembly initialize and wait for it to complete loading.
我在程序集初始化中启动应用程序并等待它完成加载。 Both when I manually close the main window (which was opened from the test) and when I close it in the assembly cleanup (automated way), after the assembly cleanup method is finished I always get the
COM object that has been separated from its underlying RCW cannot be used, only in test debug
exception.当我手动关闭主要 window (从测试中打开)和当我在程序集清理(自动方式)中关闭它时,在程序集清理方法完成后,我总是得到
COM object that has been separated from its underlying RCW cannot be used, only in test debug
异常时使用。 I tried to wait for GC to finish and also call Dispatcher.CurrentDispatcher.InvokeShutdown();
我试图等待 GC 完成并调用
Dispatcher.CurrentDispatcher.InvokeShutdown();
in the cleanup as suggested in similar threads, but no luck.在类似线程中建议的清理中,但没有运气。
Note, that in the actual test case I was not doing anything, as I wanted to minimize the scenario and make sure that this exception is thrown just by opening and closing the application.请注意,在实际测试用例中,我没有做任何事情,因为我想最小化场景并确保仅通过打开和关闭应用程序来引发此异常。 Of course, when I just run the application manually and not from the test, I don't get any exception when closing the window.
当然,当我只是手动运行应用程序而不是从测试中运行时,关闭 window 时不会出现任何异常。
Any other suggestions to prevent this?还有其他建议可以防止这种情况吗?
When running and no debugging my test, the execution completes successfully and VS can continue with running the tests of the next scheduled project (which is not related to starting the application).当运行并且没有调试我的测试时,执行成功完成并且VS可以继续运行下一个计划项目的测试(与启动应用程序无关)。
Stack trace堆栈跟踪
PresentationCore.dll!System.Windows.Input.TextServicesContext.StopTransitoryExtension()
PresentationCore.dll!System.Windows.Input.TextServicesContext.Uninitialize(bool appDomainShutdown)
WindowsBase.dll!MS.Internal.ShutDownListener.HandleShutDown(object sender, System.EventArgs e)
Repro :复制:
WpfApp1
)WpfApp1
)MainWindow.xaml
add a simple empty gridMainWindow.xaml
添加一个简单的空网格<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
Program.cs
file with the following code:Program.cs
文件:public static class Program
{
[STAThread]
public static void Startup()
{
var window = new MainWindow();
window.Show();
window.Close();
}
}
UnitTestProject1
)UnitTestProject1
)[TestClass]
public class UnitTest1
{
[AssemblyInitialize]
public static void Initialize(TestContext _)
{
var mainThread = new Thread(() =>
{
WpfApp1.Program.Startup();
});
mainThread.SetApartmentState(ApartmentState.STA);
mainThread.Start();
mainThread.Join();
}
[TestMethod]
public void TestMethod1()
{
}
}
TestMethod1
TestMethod1
Dispatcher.CurrentDispatcher.InvokeShutdown();
needs to get called within the mainThread
(right after the call to WpfApp1.Program.Startup();
)需要在
mainThread
中调用(在调用WpfApp1.Program.Startup();
之后)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.