There are many questions on SO asking same doubt. Solution for this is to set
notifyIcon.icon = null
and calling Dispose
for it in FormClo
Try Application.DoEvents();
after setting notifyIcon.Icon
to null
and disposing:
notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();
And consider Environment.Exit(0);
instead of Process.GetCurrentProcess().Kill()
.
The only way that works to me was:
On design screen changing notifyicon1 property visible=false
Insert the code below on main form "activated" event:
NotifyIcon1.Visible = True
NotifyIcon1.Visible = false
NotifyIcon1.Icon.Dispose()
NotifyIcon1.Dispose()
I had the exact same problem as you.
The proper way are send WM_CLOSE message to a process.
I use the c# code I found in this article.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way
edit codes of ...Designer.cs as below coding.
protected override void Dispose(bool disposing)
{
if (disposing )
{
this.notifyicon.Dispose();
}
base.Dispose(disposing);
}
Components just must be disposed in the right order like this :
NotifyIcon.Icon.Dispose();
NotifyIcon.Dispose();
Add this to the MainWindow
closing event.
Hope this will help.
I don't think WPF has it's own NotifyIcon, does it? If you're using the 3rd party Harcodet.Wpf.TaskbarNotification, then try this:
In order to prevent my app from closing when the window is closed (run in background), I separated the logic for closing the window (hitting the x button in the upper right) and actually shutting it down (through the context menu). To make this work, make your context menu set _isExplicitClose
to true. Otherwise, it'll just hide the window and continue to run.
What this does is, on explicit close, hide tray icon and the form before closing. This way the icon isn't hanging around after the application is shutdown.
private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (!_isExplicitClose)
{
e.Cancel = true;
Hide();
}
}
protected void QuitService(object sender, RoutedEventArgs e)
{
_isExplicitClose = true;
TaskbarIcon.Visibility = Visibility.Hidden;
Close();
}