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.


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)
            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;
        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;
            _notifyIcon = null;
        private void ShowMainWindow()
            if (MainWindow.IsVisible)
                if (MainWindow.WindowState == WindowState.Minimized)
                    MainWindow.WindowState = WindowState.Normal;
        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"


Download the sample-project here: Background application

Share this post

Comments (4)

  • 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

    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


    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:


      March 14, 2017 at 10:22 pm

Leave a Reply

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