创建一个服务类MyClass,里面包含两个方法Start和Stop
安装,卸载 Cmd命令 installutil WindowsService_test.exe (安装Windows服务) installutil /u WindowsService_test.exe (卸载Windows服务) 代码下载:https://files.cnblogs.com/aierong/WindowsService_test.rar b.利用组件Topshelf 本方式特点:代码简单,开源组件,Windows服务可运行多个实例 Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com 第1步:引用程序集TopShelf.dll和log4net.dll 第2步:创建一个服务类MyClass,里面包含两个方法Start和Stop,还包含一个定时器Timer,每隔5秒往文本文件中写入字符 。更多内容http://zhieyuoa.blog.chinaunix.net
public class MyClass { readonly Timer _timer;
private static readonly string FileName = Directory.GetCurrentDirectory ( ) + @"\" + "test.txt";
public MyClass ( ) { _timer = new Timer ( 5000 )
{ AutoReset = true , Enabled = true };
_timer.Elapsed += delegate ( object sender , ElapsedEventArgs e )
{ this.witre ( string.Format ( "Run DateTime {0}" , DateTime.Now ) ); }; }
void witre ( string context ) { StreamWriter sw = File.AppendText ( FileName );
sw.WriteLine ( context ); sw.Flush ( ); sw.Close ( ); }
public void Start ( ) { this.witre ( string.Format ( "Start DateTime {0}" , DateTime.Now ) ); }
public void Stop ( ) { this.witre ( string.Format ( "Stop DateTime {0}" , DateTime.Now ) + Environment.NewLine ); }
} 健康知识微创保胆取石手术
浙公网安备 33010602011771号