Creating a background application with WPF

Sometimes you need to have an application running in the background. Then you don’t want your MainWindow to be visible all the time. Instead you just want to have an icon in the notification area that allows the user to open up the MainWindow and to exit the application. In WPF you can do this easily with the help of Windows Forms’ NotifyIcon-class.

Just add references to System.Windows.Forms and System.Drawing to your WPF-project. Add also an icon-file (.ico) to your project resources. To do this just open the Resources.resx-file in the Properties-folder of your project. Select the Icon-resource and click the Add Resource button. I’ve named it MyIcon.

AddIconToResources

Then go to the App.xaml.cs and implement it like below. The trick is to never close the MainWindow, as a closed Window cannot be shown again. Instead cancel the closing and just hide it.

using System.ComponentModel;
using System.Windows;
 
namespace BackgroundApplication
{
 
    public partial class App : Application
    {
        private System.Windows.Forms.NotifyIcon _notifyIcon;
        private bool _isExit;
 
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow = new MainWindow();
            MainWindow.Closing += MainWindow_Closing;
 
            _notifyIcon = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.DoubleClick += (s, args) => ShowMainWindow();
            _notifyIcon.Icon = BackgroundApplication.Properties.Resources.MyIcon;
            _notifyIcon.Visible = true;
 
            CreateContextMenu();
        }
 
        private void CreateContextMenu()
        {
            _notifyIcon.ContextMenuStrip =
              new System.Windows.Forms.ContextMenuStrip();
            _notifyIcon.ContextMenuStrip.Items.Add("MainWindow...").Click += (s, e) => ShowMainWindow();
            _notifyIcon.ContextMenuStrip.Items.Add("Exit").Click += (s, e) => ExitApplication();
        }
 
        private void ExitApplication()
        {
            _isExit = true;
            MainWindow.Close();
            _notifyIcon.Dispose();
            _notifyIcon = null;
        }
 
        private void ShowMainWindow()
        {
            if (MainWindow.IsVisible)
            {
                if (MainWindow.WindowState == WindowState.Minimized)
                {
                    MainWindow.WindowState = WindowState.Normal;
                }
                MainWindow.Activate();
            }
            else
            {
                MainWindow.Show();
            }
        }
 
        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            if (!_isExit)
            {
                e.Cancel = true;
                MainWindow.Hide(); // A hidden window can be shown again, a closed one not
            }
        }
    }
}

Now go in addition to the App.xaml and remove the Startup-Uri, so that when you start the application, only the NotifyIcon is added to the notification area

<Application x:Class="BackgroundApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:BackgroundApplication">
  <Application.Resources>
         
    </Application.Resources>
</Application>

Done

Download the sample-project here: Background application

Share this post

Comments (6)

  • Apyn AOM Reply

    Thanks again ! Very fast and easy solution
    Personnaly, I use this code to set an icon :
    _notifyIcon.Icon = Propeties.Resources.MyIcon;
    After having add the icon in Project Propeties, tab Resources ;)

    August 22, 2015 at 12:47 pm
    • Thomas Claudius Huber Reply

      Jep, that’s more the Windows Forms way and great. Thanks. I’ve adjusted it in the Blog-post. :-)

      August 22, 2015 at 1:00 pm
  • Jiya Reply

    Hi,
    Great Article. It helped a lot. But i have a question.
    Since _notifyIcon is in App.xml.cs file, How to i access it in MainWindow.cs file ?
    I tried making _notifyIcon public and then tried to access it like App._notifyIcon , but property was not listed.

    When some activity happens, I want to show ShowBalloonTip from Mainwindow.cs

    please advice

    Regards
    Jiya

    March 10, 2017 at 7:23 pm
    • Thomas Claudius Huber Reply

      Hi Jiya, thanks. If you want to acces the variable, I recommend you to create a public readonly property in App.xaml.cs:

      public System.Windows.Forms.NotifyIcon NotifyIcon { get { return _notifyIcon; } }

      Then you can use it in your MainWindow like this:

      ((App)App.Current).NotifyIcon

      March 14, 2017 at 10:22 pm
  • Santiago Reply

    It works even in Windows 10, great!!!

    The only caveat is that the NotifyIcon stays in the system tray even after the application is closed, and disappears only when the system tray is reopened. To avoid it, just set the Visible property to false before disposing the NotifyIcon.

    Thank you very much.

    June 1, 2017 at 1:13 pm
    • Thomas Claudius Huber Reply

      Hi Santiago,
      you’re welcome. Thanks for the feedback, great to hear it works in Windows 10 as well. :)

      June 8, 2017 at 11:19 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*