Home > Net >  Tray Icon not showing up
Tray Icon not showing up

Time:12-21

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;
        }
    }
}

CodePudding user response:

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

StartWebServer().Wait();

Which blocks the rest of the code.

  • Related