从 .NET 库中的主线程显示 WPF 对话框
我在这里阅读了几篇文章并通过谷歌的参考文献,但还没有找到正确的解决方案。
我有一个显示对话框的程序集,但该对话框需要从主线程显示。调用者可以是 .NET 应用程序或本机应用程序。
从概念上讲,我似乎只需要获取应用程序的主线程 ID,然后调用 System.Windows.Threading.Dispatcher.FromThread( main_thread_id_here) 来获取 Dispatcher,然后调用 Invoke 从主线程显示我的对话框。但是,我一直无法弄清楚如何获取主线程ID。
我的下一个方法是(遵循另一个 SO 问题的建议)迭代 Process.GetCurrentProcess().Threads,并获取首先启动的 ProcessThread 的线程 ID。但是现在我有了这个ProcessThread,我没有办法获取相应的Thread对象。我想接下来我可以获取当前进程中所有正在运行的 .NET 线程的列表,并通过 GetHashCode 比较它们的线程 ID 以确定哪个是主线程。然后,我可以使用 FromThread
获取 Dispatcher
...但除了 ProcessThreads
之外,我无法找到任何有关检索任何内容的信息!
有没有人有关于如何使用我上面解释的方法或其他方法解决此问题的提示?请记住,我无法控制调用应用程序。
I've read several posts here and via references from google, but haven't come up with the proper solution.
I have an assembly that displays a dialog, but that dialog needs to be shown from the main thread. The caller could be a .NET application or native application.
Conceptually, it seems like I simply need to get the application's main thread ID, and then call System.Windows.Threading.Dispatcher.FromThread( main_thread_id_here)
to get the Dispatcher, and then call Invoke
to display my dialog from the main thread. However, I haven't been able to figure out how to get the main thread ID.
My next approach was to (following advice from another SO question) iterate over Process.GetCurrentProcess().Threads
, and get the thread ID of the ProcessThread
that was started first. But now that I have this ProcessThread
, I don't have a way to get the corresponding Thread
object. I figured that I could next get a list of all running .NET threads in the current process, and compare their thread IDs via GetHashCode
to determine which one is the main thread. I could then use FromThread
to get the Dispatcher
... but I haven't been able to find any information on retrieving anything but ProcessThreads
!
Does anyone have tips on how to solve this problem using either approach I have explained above, or maybe another approach? Please keep in mind that I don't have control over the calling application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以从 Window.Dispatcher 属性获取调度程序。
You could also grab the dispatcher from the
Window.Dispatcher
property.