服务制作
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.Threading.Tasks; using System.IO; using System.Timers; namespace Rhyhtmk.WindowsService { /* @ blogs: http://www.cnblogs.com/rhythmk */ public partial class MyServices : ServiceBase { private System.Timers.Timer timer1 = new System.Timers.Timer(); public MyServices() { this.timer1.Enabled = true; this.timer1.Interval = 1000D; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick); InitializeComponent(); this.ServiceName = "Rhythmk 测试服务"; } protected override void OnStart(string[] args) { this.WriteLog("Rhythmk服务启动正常!"); this.timer1.Enabled = true; this.timer1.Start(); } protected override void OnStop() { this.timer1.Enabled = false; this.WriteLog("Rhythmk服务停止服务!"); } private void timer1_Tick(object sender, ElapsedEventArgs e) { this.WriteLog("timer绑定方法被触发!"); } #region 记录日志 private void WriteLog(string msg) { msg = "Time:" + DateTime.Now.ToString() + "\r\n" + msg; string path = @"C:\bin\rhythmk.txt"; StreamWriter sw = File.AppendText(path); sw.WriteLine("----------------结果开始----------------"); sw.WriteLine(msg); sw.WriteLine("----------------结果结束----------------"); sw.Flush(); sw.Close(); } #endregion } }
Install.bat:
sc create RhythmkTask binpath= "%CD%\Rhyhtmk.WindowsService.exe" displayname= "Rhythmk.WindowsService" depend= Tcpip start= auto
sc description RhythmkTask "Rhythmk任务系统"
sc start RhythmkTask
pause
UnInstall.bat:
sc stop RhythmkTask
sc delete RhythmkTask
pause
一只站在树上的鸟儿,从来不会害怕树枝会断裂,因为它相信的不是树枝,而是它自己的翅膀。与其每天担心未来,不如努力做好现在。