码迷,mamicode.com
首页 > Windows程序 > 详细

我的第一个Windows服务

时间:2015-05-17 20:11:58      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; 

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        //定时器  
        System.Timers.Timer t = null;  

        public Service1()
        {
            InitializeComponent();
            //启用暂停恢复  
            base.CanPauseAndContinue = true;

            //每5秒执行一次  
            t = new System.Timers.Timer(5000);
            //设置是执行一次(false)还是一直执行(true);  
            t.AutoReset = true;
            //是否执行System.Timers.Timer.Elapsed事件;  
            t.Enabled = true;
            //到达时间的时候执行事件(theout方法);  
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);  
        }

        protected override void OnStart(string[] args)
        {
            string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "启动";
            WriteLog(state);  
        }

        protected override void OnStop()
        {
            string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "停止";
            WriteLog(state);  
        }
        //恢复服务执行  
        protected override void OnContinue()
        {
            string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "继续";
            WriteLog(state);
            t.Start();
        }

        //暂停服务执行  
        protected override void OnPause()
        {
            string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "暂停";
            WriteLog(state);

            t.Stop();
        }


        public void WriteLog(string str)
        {
            using (StreamWriter sw = File.AppendText(@"d:\service.txt"))
            {
                sw.WriteLine(str);
                sw.Flush();
            }
        }

        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {

            WriteLog("theout:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        }  
    }
}

 

打开ProjectInstaller,修改serviceInstaller1组件属性

Description= 我的服务备注                       服务备注说明

DisplayName=我的服务                            服务友好名字 

ServiceName=MyService                         安装服务器名字

StartType=Automatic                                服务类型

Manual      服务安装后,必须手动启动。

Automatic    每次计算机重新启动时,服务都会自动启动。

Disabled     服务无法启动。

并设计serviceProcessInstaller1的属性Account=LocalSystem

如果将Account设置成User,安装时就会:

 技术分享

安装windows服务要有installutil.exe,这个在安装vs的时候已经有了

看看自己windows服务的系统版本和net版本找到installutil.exe,

我的是在:C:\Windows\Microsoft.NET\Framework\v4.0.30319

并把它复制到新建服务的bin/debug里面,就是跟windows服务的exe在一个文件夹里,这样后面安装比较好

安装

dos到windows服务所在文件夹

installutil.exe WindowsService1.exe

技术分享

安装好了以后可以在服务里面查看

技术分享

卸载

installutil.exe -u WindowsService.exe

技术分享

 点击服务启动

报错

技术分享

 这个只要把服务权限选项卡设置everyone就可以了

查看输出文件

技术分享

第三个启动是重新启动,服务先停后启

 

http://blog.csdn.net/angle860123/article/details/17375895

我的第一个Windows服务

标签:

原文地址:http://www.cnblogs.com/hongdada/p/4510255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!