static void Main(string[] args) { var rc = HostFactory.Run(x => //1 { x.Service<SyncData>(s => //2 { s.ConstructUsing(name => new SyncData()); //3 s.WhenStarted(tc => tc.Start()); //4 s.WhenStopped(tc => tc.Stop()); //5 }); x.RunAsLocalSystem(); //6 x.SetDescription("同步服务"); //7 x.SetDisplayName("SyncData"); //8 x.SetServiceName("SyncData"); //9 }); //10 var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); //11 Environment.ExitCode = exitCode; } public class SyncData { readonly Timer _timer; public SyncData() { _timer = new Timer(r => { var fs = File.AppendText("X:\\test.txt"); fs.Write("It is {0} and all is well.\r\n", DateTime.Now); fs.Close(); }, null, 0, 1000); } public void Start() { } public void Stop() { _timer.Change(-1, 1000); } }
安装方法: text.exe install
运行方法: text.exe start
停止方法: text.exe stop
卸载方法: text.exe uninstall