James Buttler
James Buttler

Reputation: 61

How to dispose of NotifyIcon, after the timeout has occured (C#)

I have a NotifyIcon method, although I would like the timeout to happen, before disposing of the BaloonTip.

private void button1_Click(object sender, EventArgs e)
{
    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(30000);
    <wait until timeout occurs>
    notifyIcon1.Dispose();

}

Upvotes: 1

Views: 1022

Answers (3)

ba__friend
ba__friend

Reputation: 5913

I would rather hide the NotifyIcon instead of recreating/disposing a new instance of it.

Upvotes: 1

agent-j
agent-j

Reputation: 27943

notifyIcon1.BalloonTipClosed += delegate {notifyIcon1.Dispose ();};

Upvotes: 3

Hertzel Guinness
Hertzel Guinness

Reputation: 5950

Try using a timer.
Should be something like...:

private Timer taskTimer;
private NotifyIcon notifyIcon1;

private void button1_Click(object sender, EventArgs e)
{
    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(30000);
    taskTimer = new Timer(TimerCallback, notifyIcon1, 30000, System.Threading.Timeout.Infinite);
}

and...

void TimerCallback(object notifyIcon1Obj)
{
    lock (notifyIcon1Obj) 
    {
        NotifyIcon notifyIcon1 = (NotifyIcon)notifyIcon1Obj;
        notifyIcon1.dispose();
        notifyIcon1 = null;
    }
}

HTH

Upvotes: 0

Related Questions