Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Dump MySQL & restore $ mysqldump -u root -x --all-databases > dump.sql $ mysqldump -u root データベース名 > dump.sql $ mysql -u root -p $ mysql -u root データベ... Create barcode import java.io.File; import java.io.FileNotFoundException; import net.sourceforge.barbecue.Barcode; import net.sourceforge.barbecue.Barcode...
Stats
Window service sample
First, create a new window service project.

For schedule service, we need to add System.Timers.Timer to toolbox, and drag it into design mode.

Then, code something.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowServiceSample
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // 服務啟動時執行
            this.EventLog.WriteEntry(System.AppDomain.CurrentDomain.BaseDirectory);  // write current directory to event log
        }

        protected override void OnStop()
        {
            // 服務結束時執行
        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 計時器, 依據設定間隔執行
        }
    }
}
After coding, we need to add a installer for Window Service Installer.

There are two components needed to set, serviceProcessInstaller and serviceInstaller

Build the project and find the executable binary, enter the command below for installing.
%windir%\Microsoft.NET\Framework\v2.0.50727\installutil WindowServiceSample.exe
There will be a new service listed in Services.

For unstalling, enter command below:
%windir%\Microsoft.NET\Framework\v2.0.50727\installutil /u WindowServiceSample.exe