⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Creating Your Own Service in C#

Here, I won't provide detailed instructions on which button to click and which menu to select in Visual Studio. I'll focus on the two main problematic issues I encountered when creating my own service.

First Issue. The service itself should do something, which is usually done by a timer. However, the timer didn't work. It turned out that the standard System.Windows.Forms.Timer works only based on window messages! I found information that you can also use System.Timers.Timer or System.Threading.Timer, but they didn't work either. I returned to the first option and created a form and timer in a separate thread, as shown below.

public partial class Service1 : ServiceBase
    {
        System.Windows.Forms.Timer timer1;
        System.Windows.Forms.Form form1;
        System.Threading.Thread thread1;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //При запуске службы запускаем новый поток 
            thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
            thread1.Start();
            thread1.IsBackground = true;

        }


        void ThreadProc()
        {
            //Создаем во вновь созданном потоке форму и таймер 
            form1 = new System.Windows.Forms.Form();
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer_Tick);
            timer1.Interval = 2000;
            timer1.Start();
            form1.Show();
            //Запускаем всё это добро. 
            System.Windows.Forms.Application.Run(form1);
        }


        protected override void OnStop()
        {

            thread1.Abort();

        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer1.Stop();
            runProcedure(); //Запускаем процедуру (чего хотим выполнить по таймеру).
            timer1.Enabled = true;
            timer1.Start();

        }
    }        


The timer started working in the service, nothing complicated, just a simple solution.
But that wasn't the end of it!

Second Issue!
Now, how do I install this? So that it runs and works in the services.
To install the C# service, you need the «InstallUtil.exe» utility, which is located in the .NET 2 folder. I found it in the following path: C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727. I had multiple versions, but the required file was there.

To avoid specifying paths, I copied the InstallUtil.exe and InstallUtilLib.dll files from this folder to the project folder with the compiled service and ran the installation with the following command:

InstallUtil.exe /i MyService.exe


However, this won't work if you don't add a Class Installer to the project first. This is done by right-clicking on an empty space in the Service Designer (in my case, the Service1.cs[Design] tab) and selecting the «Add Installer» menu item. Essentially, that's it. After this action, the «Project Installer» tab will appear, where you can set the service name and description in the ServiceInstaller properties, and the account under which the service will run in the ServiceProcessInstaller properties. If you set the Account property to User, the service will prompt for a username and password during installation.

That's it.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.