0

I have added a tray icon to my program that should show the up and have buttons for toggling certain functionality. However, the tray icon is not showing up.

I have checked that System.Windows.Forms is included, that the Application.Run() method is called after creating the tray icon, that the Visible property of the NotifyIcon object is set to true, that the Icon property is set correctly (tried several different ones, SystemIcons, my application icon, a specified file), and that the Text property is set properly.

I have looked at various SO questions & answers to no avail, it's a Windows Application targeting .NET Framework 4.7.2, it does not utilize a form if that matters.

using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SomethingSomething
{
    internal static class Program
    {
        static NotifyIcon trayIcon;
        [STAThread]
        static void Main()
        {
            // Start the webserver
            StartWebServer().Wait();

            // Create the tray icon
            CreateTrayIcon();

            // Needed for tray icon
            Application.Run();
        }

        static void CreateTrayIcon()
        {
            trayIcon = new NotifyIcon
            {
                Visible = true,
                Icon = SystemIcons.Information,
                Text = "Current Song" + currentSong
            };

            var menu = new ContextMenu();
            var toggleRPCMenuItem = new MenuItem("Toggle RPC", (s, e) => ToggleRPC());
            var toggleAdsMenuItem = new MenuItem("Toggle Ads", (s, e) => ToggleAds());
            var exitMenuItem = new MenuItem("Exit", (s, e) => Exit());
            menu.MenuItems.Add(toggleRPCMenuItem);
            menu.MenuItems.Add(toggleAdsMenuItem);
            menu.MenuItems.Add(exitMenuItem);
            trayIcon.ContextMenu = menu;
        }
    }
}
S. C.
  • 110
  • 1
  • 13
  • 1
    Why are you creating the trayicon and context menu in `Program` anyway? It's much easier to do all this from the WinForms designer and ensures it is pumped correctly. You can always make your main window invisible. –  Dec 18 '22 at 06:03

1 Answers1

0

Turns out I had a method that ran before CreateTrayIcon();:

StartWebServer().Wait();

Which blocks the rest of the code.

S. C.
  • 110
  • 1
  • 13
  • 1
    So what did you do? End up removing the WS entirely or did you remove the `Wait()`. If the latter, does the WS still function? –  Dec 18 '22 at 05:44
  • I removed .Wait(); and yes, it still functions. – S. C. Dec 18 '22 at 14:56