可以在另一个线程的 Win32 Dll 中使用 C# 面板的 HWND 吗?

发布于 2024-12-16 20:44:04 字数 374 浏览 1 评论 0原文

我有一些东西可以工作,但我不确定它是否正常以及以后是否会引起问题。

我有一个 Win32 C++ 应用程序,它执行一些视频处理并使用 Direct3D 进行显示(不是我的代码)。我的任务是用 C# GUI 替换 GUI。

我已经将 C++ 代码制作成 dll 了。 我用 C# 创建了一个 GUI,并有一个用于视频显示的面板。我将此面板的 HWND 传递给 dll,一切似乎都工作正常。

但是 - 该面板正在被创建该面板的线程以外的线程访问,这是不应该执行的。
我内心的一部分想要“别管它”,但这感觉不对,我怀疑这会导致问题。例如,当我们想要捕获面板上的点击事件时,该事件将位于创建面板的主线程或 UI 线程上。

关于我应该如何做这件事有什么建议吗? 谢谢 谢谢

I have something working but I'm not sure it is OK and if it will cause problems later on.

I have a Win32 C++ app which does some video processing and uses Direct3D for display (not my code). My task is replacing the GUI with a C# GUI.

I've made the C++ code into a dll.
I've created a GUI in C# and have a panel intended for the video-display. I pass this panel's HWND to the dll, and everything seems to work just fine.

But - that panel is being accessed by a thread other than the one it was created on, which shouldn't be done.
Part of me wants to "leave well enough alone" but this feels wrong and I suspect it will cause problems down the line. Such as when we want to catch click-events on the panel, which will be on the main or UI thread ... which created the panel.

ANy suggestions on how I should be doing this?
Thank you
Thanks

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

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

发布评论

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

评论(2

流心雨 2024-12-23 20:44:04

我想你已经知道这个问题的答案了。 Windows 具有线程关联性。窗口上的所有操作都应该从创建它的线程中执行。

鉴于这条规则,我无法想象 DLL 负责将窗口访问移动到不同的线程上,并假设是您的代码执行此操作。解决方案是在调用 DLL 的同一线程中创建和访问窗口。

I think you already know the answer to this. Windows have thread affinity. All operations on a window should be performed from the thread that created it.

Given this rule, I can't imagine that the DLL is responsible for moving window access onto a different thread and presume it is your code that does it. The solution is for you to create and access the window in the same thread that calls the DLL.

夜声 2024-12-23 20:44:04

我发现这种技术的主要问题是用户界面可能会闪烁。如果您很早就开始使用 WPF,您可能需要查看 D3DImage 如果不是你现在所做的大部分工作。

The main problem I've seen with this technique is that your UI can flicker. If you are early enough in the process to use WPF you may want to take a look at D3DImage if not what you are doing now will mostly work.

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