将 Invoke 与扩展应用程序上下文的 UI 元素一起使用?

发布于 2024-09-07 06:09:30 字数 468 浏览 3 评论 0原文

我有一个扩展应用程序上下文的 taskBarIcon 元素,我的整个 UI 都是从该元素设计的。这是因为应用程序的原始功能是运行后台进程并简单地提供图标作为应用程序正在运行的确认。 但是,我现在以菜单的形式添加附加功能,可以通过右键单击图标来访问该功能,并且由于核心功能在一个线程上运行,并且图标在主线程上运行,因此我在访问图标时遇到问题在需要显示通知气泡的简单情况下。两个线程没有其他可能访问同一内存的点,因此同步并不是真正的问题。

我习惯了 Java,其中这个过程要简单得多。我读过有关 C# 所需的 Invoke-Delegate dance 的文章,但没有一篇专门针对扩展应用程序上下文的对象。我当前的实现过于不可预测,无法确保生产安全。

我的问题是:如何将消息发送到其他进程以显示通知气泡,而不影响菜单的可访问性?具体来说,一个带有扩展应用程序上下文的 UI 类和调用调用方法的单独线程上的类的简单示例将非常有帮助。

我真的很感谢任何帮助!

坏熊猫

I have a taskBarIcon element extending Application Context from which my entire UI is designed. This is because the original functionality of the application was to run a background process and simply provide the icon as a confirmation that the app was running.
However, I am now adding additional functionality in the form of a menu that may be accessed by right clicking the icon, and since the core functionality is running on one thread, and the icon on the main thread, I am having issues accessing the icon in the simple case of needing to display a notification bubble. There are no other points at which the two threads may be accessing the same memory, so synchronization is not really an issue.

I am used to Java, in which this process is far simpler. I've read the articles I can find regarding the Invoke-Delegate dance that C# requires, but none are specific to an object extending Application Context. My current implementation is far too unpredictable to be production safe.

My question is this: How do I send the message to the other process to display the notification bubble, without disturbing the accessibility of the menu? Specifically, a simple example with a UI class extending Application Context and a class on a separate thread calling the invoke method would be very helpful.

I really appreciate any help!

badPanda

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

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

发布评论

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

评论(1

魂归处 2024-09-14 06:09:30

您也可以在创建通知气泡的同一线程上使用分配给 SynchronizationContext.CurrentSynchronizationContext 对象。然后,您可以将 SynchronizationContext 对象传递到菜单所在的任何组件中,并且它将使用 context.Send(....) 发送消息。或者,如果您有权访问通知气泡组件或其所在的表单,则可以执行 form.Invoke((MethodInvoker)delegate {....});

You could just as well use a SynchronizationContext object that you assign to SynchronizationContext.Current on the same thread that you create the notification bubble. You would then pass your SynchronizationContext object into whatever component the menu is on and it would use context.Send(....) to send a message. Or, if you have access to the notification bubble component or the form it's on, you could do form.Invoke((MethodInvoker)delegate {....});

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