Windows Service的建立、启动与调试

      今天有用到Windows Service,于是写了简单的应用,也碰到了不少问题,简单总结一下。
一、建立Windows Service
      新建工程选择windows service,会自动生成Service1,这里新建了一个FirstService,在windows service中经常用timers来控制service的运行。以下是代码
ContractedBlock.gif ExpandedBlockStart.gif Code
    partial class FirstService : ServiceBase
ExpandedBlockStart.gifContractedBlock.gif    
{
        System.Timers.Timer timer 
= new System.Timers.Timer();
        
string path = ConfigurationManager.AppSettings["path"];
        
public FirstService()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
protected override void OnStart(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            timer.Elapsed 
+= new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval 
= 1000 * 5;
            timer.Enabled 
= true;
        }


        
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            timer.Enabled 
= false;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                SaveLog(
"hello world");
            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                SaveLog(ex.Message);
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                timer.Interval 
= 1000 * 5;
                timer.Enabled 
= true;    
            }

            
        }


        
protected override void OnStop()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            timer.Enabled 
= false;
        }


        
private void SaveLog(string input)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string logPath = path + DateTime.Now.ToString("yyyy-MM-dd"+ ".txt";
            FileStream fs 
= new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw 
= new StreamWriter(fs);
            sw.BaseStream.Seek(
0, SeekOrigin.End);
            sw.Write(
"Began Entry:");
            sw.Write(
"\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"+ " " + input);
            sw.WriteLine();
            sw.Flush();
            sw.Close();
        }

    }
这个service要实现的就是每隔5秒钟向一个txt文件中写入"hello world"
完成之后在该service的设计器上右键单击,选择"Add Installer",会生成对应的project installer,注意它包含了两个installer
serviceProcessInstaller1的Account属性为该service在哪种帐户类型下运行,local stystem为本地系统,user为登入用户
serviceInstaller1的ServiceName属性为对应的service名字,即FirstService
二、安装、启动Windows Service
      建置工程,通过以后,就可安装了。安装的常用命令如下:
      1.安装Service:InstallUtil FirstService.exe
      2.卸载Service:InstallUtil /u FirstService.exe
      3.启动Service:Net start FirstService
      4.停止Service:Net stop FirstService
      注意:这些命令均在.net framework的命令提示符下运行,启动与停止service也可以在服务列表中手动实现。
三、调试Windows Service
      windows service不能直接调试,参照了一下众网友的做法,主要有一下几种:
      1.写日志。即在自己觉得会出错的地方把信息写入日志,类似以上实现的service功能。但是无法单步调试,不方便
      2.附加进程。这样可以象正常的windows application一样进行单步调试,但是,必须在启动服务之后,才能附加进程。附加进程时选择显示该用户的所有进程即可显示service的进程,如图所示
      
      如果需要在启动时就进行调试,则需要在OnStart启动方法中添加延时处理,但是不能超过30秒,因为服务的最大启动时间为30秒,超过会报错。
      3.注释掉原Main方法中的代码,自己手动添加启动代码。
      注释掉以下代码      
ServiceBase[] ServicesToRun;
ServicesToRun 
=   new  ServiceBase[]{ new  FirstService()};
ServiceBase.Run(ServicesToRun);
      改为
FirstService firstService  =   new  FirstService();
firstService.OnStart();
      注意把protected override void OnStart(string[] args)改为public void OnStart()
      此方法需要停止服务。

转载于:https://www.cnblogs.com/youngberry/archive/2009/09/23/1572921.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值