Multiple notification icons appear when using multithreading

依然范特西╮ 提交于 2019-12-12 15:13:06

问题


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!


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5989648/multiple-notification-icons-appear-when-using-multithreading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!