Notifyicon creates multiple icons that do not disappear with program shutdown

Asked

Viewed 62 times

0

I have a Windows Form where I use the Notifyicon component.

When running the software the software by Visual Studio and finish running with the stop debug button:

botão de parar depuração

the icon is not removed until I pass the mouse over it:

Múltiplos ícones de instâncias finalizadas do programa

I’ve tried following the instructions of some stackoverflow questions but none helped me. I’ll leave the title with their links:

Notifyicon remains in Tray Even after application closing but disappears on Mouse Hover

Issue with Notifyicon not disappearing on Winforms App


As requested the code for the question:

Code snippet responsible for controlling Notifyicon compounding :

private bool allowVisible;     // ContextMenu's Show command used
private bool allowClose;       // ContextMenu's Exit command used

protected override void SetVisibleCore(bool value)
{
    if (!allowVisible)
    {
        value = false;
        if (!this.IsHandleCreated) CreateHandle();
    }

    UpdateBase(value);
}

private void UpdateBase(bool value)
{
    if (base.InvokeRequired)
    {
        var d = new Action<bool>(UpdateBase);
        base.Invoke(d, new object[] { value });
    }
    else
    {
        base.SetVisibleCore(value);
    }
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (!allowClose)
    {
        this.Hide();
        e.Cancel = true;
    }
    base.OnFormClosing(e);
}

private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
    allowVisible = true;
    Show();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    allowClose = true;
    Application.Exit();
}

Other things associated:

private void Form1_FormClosed_1(object sender, FormClosedEventArgs e)
{
    //Outras coisas sendo finalizadas
    notifyIcon1.Visible = false;
    notifyIcon1.Dispose();
}
  • 1

    Do not use C#, but as a general rule you have to release it before terminating the application. If the app is breaking the icon will stay even, regardless of the development language. I suggest you put a routine to end the icon component next to the application or form completion routine.

  • I’ve tried something like this before, but for some reason he didn’t go through the component shutdown routine when the program closed. I tried using a method despose and a destructor in the c++style. I will try this approach again because it may have failed due to some deployment error.

  • Show the code

  • I will post today. I have to select the appropriate snippets for being proprietary software.

  • Add @Leandroangelo

  • Does this behavior only happen when you’re debugging the code? If so, I don’t think you need to worry

  • If I close the application through the task manager too. But yes it is only in these cases.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.