Click here to Skip to main content
15,886,060 members
Articles / Programming Languages / C#

Create a system tray icon and a dialog for a Windows Service

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
25 Jan 2008CPOL2 min read 366.3K   7.3K   120  
This article shows how to create a system tray icon and a dialog for a Windows Service.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace SystemTrayIconInSvc
{
    public partial class Svc : ServiceBase
    {
        private SensAdvisor m_Advisor = new SensAdvisor();
        private List<SettingDlg> m_Dlgs = new List<SettingDlg>();

        public Svc()
        {
            InitializeComponent();

            m_Advisor.OnShellStarted += this.PostShell;
        }

        internal void DebugStart()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {
            // if the service started after the windows logon
            m_Dlgs.Add(SettingDlg.StartUIThread());
        }

        protected override void OnStop()
        {
            foreach (SettingDlg dlg in m_Dlgs)
            {
                try
                {
                    dlg.Close();
                    dlg.Dispose();
                }
                catch { }
            }

            m_Dlgs.Clear();
        }

        /// <summary>
        /// Called when the shell is ready
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void PostShell(object sender, SensLogon2EventArgs e)
        {
            m_Dlgs.Add(SettingDlg.StartUIThread());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions