.net 单元测试崩溃并显示“无法跨 AppDomains 传递 GCHandle”当从外部线程调用时

发布于 2024-08-21 09:37:53 字数 526 浏览 7 评论 0原文

我正在编写一个 C# 单元测试来测试涉及线程的 C++/CLI 功能。

C++/CLI 代码实现 DirectShow 过滤器,即用于渲染电影的 Windows API。因此,我创建 DirectShow 对象,告诉它通过我的 C++/CLI 过滤器运行 AVI,等待渲染完成,然后退出。我的过滤器有一个回调,将视频帧提供给 C# 进行处理。 DirectShow 的工作方式是创建自己的线程并从该线程调用我的 COM 对象。

现在,当我正常运行代码时,这些东西可以工作,但是当从 Resharper 运行单元测试时,它会失败并显示错误“无法跨 AppDomains 传递 GCHandle”。

问题似乎在于 Resharper 在其测试运行程序中使用 AppDomains,而 DirectShow 线程不知何故与此应用程序域没有关联。

那么如何让 Resharper 进行此测试呢?是否有 NUnit/Resharper 设置来控制是否使用应用程序域?我可以以某种方式告诉 CLR 线程与特定的应用程序域关联吗?您还知道其他实用的解决方法吗?

蒂亚·简

I am writing a C# unit test to test C++/CLI functionality that involves threads.

The C++/CLI code implements a DirectShow filter, the Windows API for rendering movies. This works thus that I create DirectShow objects, I tell it to run an AVI through my C++/CLI filters, waits until rendering is done, and then exits. My filter has a callback that gives the video frames to C# for processing. DirectShow works thus that it creates its own thread and calls my COM objects from this thread.

Now, this stuff works when I run my code normally, but when running a unit test from Resharper it fails with the error "Cannot pass a GCHandle across AppDomains".

What appears to go wrong is that Resharper uses AppDomains in its testrunner, and the DirectShow thread is somehow not associated with this appdomain.

So how do I make this test work from Resharper? Is there a NUnit/Resharper setting to control if appdomains are used? Can I tell the CLR somehow that a thread is assocated with a particular appdomain? Do you know any other pragmatic workarounds?

TIA Jan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2024-08-28 09:37:53

我设法让它按照 http://www.lenholgate.com/archives/ 中描述的程序工作000856.html

这描述了如何使非托管函数指针指向托管类中的方法。当您调用此函数指针时,您将进入最初创建该对象的托管应用程序域。这修复了我的应用程序域错误。

I managed to get it working with the procedure described in http://www.lenholgate.com/archives/000856.html

This describes how to make an unmanaged function pointer to a method in a managed class. When you call this function pointer, you get in the managed appdomain where the object originally was created. This fixes my appdomain errors.

荒芜了季节 2024-08-28 09:37:53

您没有提到您正在使用的 ReSharper 版本。尝试使用最新的 ReSharper 5.0 Nightly Build 运行测试。那里的人重写了测试运行程序来天真地支持 Nunit。如果这不起作用,我建议您在他们的新错误跟踪系统。

You didn't mention the ReSharper version that you are using. Try running the tests with the latest ReSharper 5.0 Nightly Build. The guys there had rewritten the test runner to support Nunit naively. If this doesn't work, I suggest you report this as an issue at their new Bug Tracking system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文