Search code examples
.netvb.netwinformsmultithreadingnotifyicon

Multiple notification icons appear when using multithreading


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!


Solution

  • 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.