使用多线程时出现多个通知图标

发布于 2024-11-07 04:07:46 字数 564 浏览 5 评论 0原文

上下文: 我正在使用一个相对简单的 winforms 应用程序,该应用程序是在 Visual Studio 2010 中的 .NET 3.5 框架上用 VB.NET 编写的。

问题: 当程序打开时,FormLoad 事件创建两个线程。一个处理自动更新检查,另一个执行一项耗时的任务,将文件与互联网同步。这些线程的初始化如下:

   Dim update_check_thread As New Threading.Thread(AddressOf auto_update_check)
   update_check_thread.IsBackground = True
   update_check_thread.Start()

窗体还使用 NotifyIcon 控件在任务栏上绘制通知图标。不幸的是,每个启动的线程都会导致应用程序在任务栏上绘制一个附加图标。当程序打开后使用任何线程函数时,(有时)会绘制附加图标。

有没有办法“限制”表单允许绘制的图标数量?我尝试将代码移至后台工作人员,但同样的事情仍然发生。

提前致谢!

Context:
I'm working with a relatively simple winforms application, written in VB.NET on the .NET 3.5 framework in Visual Studio 2010.

Issue:
The FormLoad event creates two threads when the program is opened. One handles automatic update checking and the other performs a time consuming task syncing files with the internet. These threads are initialized as follows:

   Dim update_check_thread As New Threading.Thread(AddressOf auto_update_check)
   update_check_thread.IsBackground = True
   update_check_thread.Start()

The form also uses the NotifyIcon control to draw a notification icon on the taskbar. Unfortunately, each thread started causes the application to draw an additional icon to the taskbar. Additional icons are drawn (sometimes) when any threaded function is used after the program is opened.

Is there a way to "throttle" the number of icons that the form is allowed to draw? I've tried moving the code to a background worker, however the same thing continues to happen.

Thanks in advance!

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

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

发布评论

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

评论(3

灯下孤影 2024-11-14 04:07:46

这是 VB.NET 中常见的问题。它支持可怕的“使用类名作为对象”语法,如 Form1.Show()。当您使用线程时,这总是会导致麻烦,引用类名,就像在线程上使用时创建 Form1 类的新实例一样。另一种形式,它是不可见的,因为它的 Show() 方法从未被调用。但您确实看到了额外的 NotifyIcon。你必须解决这个问题,它还会引起其他麻烦,因为无论你认为对可见形式做什么,实际上都会发生在不可见形式上。

将 Sub New 添加到类中并在其上设置断点以查找执行此操作的代码。

This is a common kind of problem to have in VB.NET. It supports the horrid 'use the class name as an object' syntax, like Form1.Show(). This invariably causes trouble when you use threads, referencing the class name like that creates a new instance of the Form1 class when used on a thread. Another form, it isn't visible because its Show() method was never called. But you do see the extra NotifyIcon. You'll have to fix this, it causes other trouble as well because whatever you thought you'd do to the visible form actually happens on the invisible one.

Add Sub New to the class and set a breakpoint on it to find the code that does this.

送君千里 2024-11-14 04:07:46

我做了另一种解决方案,不太优雅但更简单,
我将通知图标控件放在我从未使用过的窗体中:)
我用 FrmIcon.NotifyIcon1.Visible=True 显示它

I have do another solution less elegant but more easy,
I put the notify icon control in a form that I never use :)
and I show it with FrmIcon.NotifyIcon1.Visible=True

留一抹残留的笑 2024-11-14 04:07:46

这是一个旧线程,但我的解决方案是在设计器属性面板中设置“NotifyIcon.Visible = False”,并在 Form 的 Load() 事件中添加“NotifyIcon.Visible = True”。

This is an old thread, but my solution was to set "NotifyIcon.Visible = False" in the designer property panel, and add "NotifyIcon.Visible = True" in Form's Load() event.

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