zoukankan      html  css  js  c++  java
  • 自定义Window 服务

      自定义window 服务

    开发到使用的流程:

    1、完成对应的代码之后(代码在底下),右键MyService.cs 添加安装程序

    2、添加window服务安装程序
    打开Service1.cs【设计】页面,点击右键,选择【添加安装程序】,会出现serviceInstaller1和serviceProcessInstaller1两个组件
    将serviceProcessInstaller1的Account属性设为【LocalSystem】, serviceInstaller1的StartType属性设为【Automatic】,ServiceName属性可设置服务名称,此后在【管理工具】--》【服务】中即显示此名称
    ProjectInstaller.cs文件,在安装服务后一般还需手动启动(即使上述StartType属性设为【Automatic】),可在ProjectInstaller.cs添加如下代码实现安装后自动启动
    public ProjectInstaller()
    {
    InitializeComponent();
    this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
    }

    private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
    {
    //参数为服务的名字
    System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("服务名称");
    controller.Start();
    }
    三、安装、卸载window服务
    1、输入cmd(命令行),
    4.0:cd C:WINDOWSMicrosoft.NETFrameworkv4.0.30319
    // 2.0:cd C:WINDOWSMicrosoft.NETFrameworkv2.0.50727
    2、安装服务(项目生成的exe文件路径)
    InstallUtil E:WindowsService1inDebugWindowsService1.exe
    3、卸载服务
    InstallUtil /u E:WindowsService1inDebugWindowsService1.exe

    调试,是在vs 中附加到进程的做法,或者是Debuger

    安装要有完整的bin文件下Debug的文件

    设置原始服务路口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;

    namespace WindowsService
    {
    static class Program
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    static void Main()
    {
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
    new MyService()
    };
    ServiceBase.Run(ServicesToRun);
    }
    }
    }

    (具体定义的window 服务代码)

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

    namespace WindowsService
    {
    public partial class MyService : ServiceBase
    {
    System.Timers.Timer myTimer;//定时器

    public MyService()
    {
    InitializeComponent();
    }
    int index =0;
    protected override void OnStart(string[] args)
    {
    myTimer = new System.Timers.Timer();
    myTimer.Interval = 3000;//毫秒为单位
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyEvent);//这里要加入我们执行的操作
    myTimer.Enabled = true;//允许触发相关事件,本质是多播委托(对应的委托事件)
    }

    //自定义操作
    public void MyEvent(object sender, ElapsedEventArgs e)
    {
    ProductDemoEntities db = new ProductDemoEntities();
    if (index == 0) {

    index= db.Products.OrderByDescending(x => x.Id).FirstOrDefault().Id;

    }else{

    index++;
    }

    Product product = new Product();
    product.Id = index;
    product.Name = "空" + index;
    product.Category = "自动加入" + index;
    product.Price =Convert.ToDecimal( 1.2 + index);


    db.Entry<Product>(product).State = EntityState.Added;//entry,set,查询在修改,异常在修改
    //db.Set<Product>(product).Add(product);
    // db.Products.Add(product);
    db.SaveChanges();

    }

    protected override void OnStop()
    {
    }
    }
    }

  • 相关阅读:
    HDU 1874 畅通工程续(dijkstra)
    HDU 2112 HDU Today (map函数,dijkstra最短路径)
    HDU 2680 Choose the best route(dijkstra)
    HDU 2066 一个人的旅行(最短路径,dijkstra)
    关于测评机,编译器,我有些话想说
    测评机的优化问题 时间控制
    CF Round410 D. Mike and distribution
    数字三角形2 (取模)
    CF Round410 C. Mike and gcd problem
    CF Round 423 D. High Load 星图(最优最简构建)
  • 原文地址:https://www.cnblogs.com/linbin524/p/4767109.html
Copyright © 2011-2022 走看看