http://www.cnblogs.com/lidaohang/archive/2011/01/23/1942536.html
因为需要每天定时更新数据,所以决定写成一个windos 服务。
1。在VS2008创建windows service工程
文件---新建---项目----windows服务,名称AdioService
2。结构如下图所示,双击service1.cs,在onstart中写具体代码,注意如果代码执行需要很长时间,则需要将方法放在子线程中,否则windows服务会启动不起来。
3。定时器Quartz的使用
protected override void OnStart(string[] args)
{
Thread th = new Thread(delegate()
{
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Program));
logger.Info("开始进入服务");
while (true)
{
try
{
ManInfo.Start();
}
catch (Exception ex) { logger.Error(ex); }
//
}
});
th.Start();
}
4。添加安装程序
在AdioService.cs[设计]右键---添加安装程序,会出现
注意:account选择localhost ;StartType选择Automatic(开机自动执行)
5。安装服务
开始-运行-cmd
cd C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727
找到debug下的.exe,复制路径(例如 C:/Documents and Settings/Administrator/桌面/Audiomonitor/d1/AudioService/bin/Debug/AudioService.exe")
安装命令
InstallUtil "C:/Documents and Settings/Administrator/桌面/Audiomonitor/d/AudioService/bin/Debug/AudioService.exe"
卸载命令
InstallUtil /u "C:/Documents and Settings/Administrator/桌面/Audiomonitor/d1/AudioService/bin/Debug/AudioService.exe"
在控制面板---管理工具-----服务----右键----启动服务
6。异常情况
如果不能正常启动服务,说明服务有错误。可以利用“日志查看器”查看错误信息。(调试比较麻烦)。
注意每次对服务改动的话,都必须重新安装。
7。想要服务在遇到异常停止后自动重启
找到 服务-----右键----属性---恢复-----重新启动服务