Question

I have an application in .net where I want it to always open without any manual interaction.

In this application I have used NotifyIcon so it always start in Taskbar tray but the notification icon will only show if I manually open that .exe.

so what I did is just added it in Autostart application registry entry with the help of below:

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());

So this works fine and on reboot it successfully open it in system taskbar Process list but not as a taskbar tray icon.

Can anyone help me.?

Était-ce utile?

La solution 2

There was an issue with the way I was using the icon.

There might be an issue with the "icon" file we use in NotifyIcon so I just fixed that issue just by replacing the way

// START: Creating a red graphic instead of image
Bitmap b = new Bitmap(16, 16);

Graphics g = Graphics.FromImage(b);
g.Clear(Color.Transparent);
SolidBrush sb = new SolidBrush(Color.Red);
g.FillEllipse(sb, 0, 0, 16, 16);
// END: Creating a red graphic instead of image

m_notifyicon.Visible = true;
m_notifyicon.Icon = Icon.FromHandle(b.GetHicon());

Now I am able to see the Red icon even after rebooting my computer.

Autres conseils

I'm working with NotifyIcon too, and there are some issues with that. First, you need to set an Icon for the NotifyIcon and be sure you didn't set it's Visibility to anything but Visibility.Visible.

Then, NotifyIcon is just a wrapper around the NotifyIcon Windows API, and there's a known issue that it cannot always be created. Therefore, when you initialize the NotifyIcon it might throw an Exception because of an error in Windows (the WinApi returns false if it couldn't be created, and in the source code they throw an Exception there). When it does that, you can just recreate the NotifyIcon in a loop until it can be created.

I also saw an issue sometime when the NotifyIcon was not created in the app.xaml as a XAML object but in code, since then I always create it in XAML instead of in code. Also now I imported the whole NotifyIcon project from CodeProject to be able to debug it's interior. So now I'm creating it in this way:

        <NotifyIcon1:NotifyIcon x:Key="NotifyIcon" x:Name="notifyicon"
                        ToolTipText="" Visibility="Visible" IconSource="/Images/Icons/bulb.ico"/>

This should throw an Exception if the icon cannot be created in this part of the code in the NotifyIcon library:

/// <summary>
/// Creates the taskbar icon. This message is invoked during initialization,
/// if the taskbar is restarted, and whenever the icon is displayed.
/// </summary>
private void CreateTaskbarIcon()
{
  lock (this)
  {
    if (!IsTaskbarIconCreated)
    {
      const IconDataMembers members = IconDataMembers.Message
                                      | IconDataMembers.Icon
                                      | IconDataMembers.Tip;

      //write initial configuration
      var status = Util.WriteIconData(ref iconData, NotifyCommand.Add, members);
      if (!status)
      {
        throw new Win32Exception("Could not create icon data");
      }

      //set to most recent version
      SetVersion();
      messageSink.Version = (NotifyIconVersion) iconData.VersionOrTimeout;

      IsTaskbarIconCreated = true;
    }
  }
}

Either you directly edit the code to your needs or you try to recreate the notifyicon when there's an Exception.

I guess this will be the problem because it was the same for us, as sometimes after startup Windows is not yet ready to create the icon. Should there be another issue for you, please post the code you use to create the notifyicon and the system (XP? 64bit?) on which the problem arises.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top