自定义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:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
// 2.0:cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
2、安装服务(项目生成的exe文件路径)
InstallUtil E:\WindowsService1\bin\Debug\WindowsService1.exe
3、卸载服务
InstallUtil /u E:\WindowsService1\bin\Debug\WindowsService1.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()
{
}
}
}

自定义Window 服务的更多相关文章

  1. C# 编写Window服务基础(一)

    一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...

  2. window 服务(一)

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

  3. C#编写window服务,一步一步(1)

    Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...

  4. WPF Window 服务安装

    一.安装服务 1.已管理员的身份启动CMD 2.输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车 3.输入 InstallUtil.exe ...

  5. SharePoint 2013 中自定义WCF服务

    在使用SharePoint2013的时候,如果其他客户端 API 的组合不足,可以通过自定义 Web 服务扩展 SharePoint.默认情况下,SharePoint 2013 不仅支持创建自定义 A ...

  6. 自定义Attribute 服务端校验 客户端校验

    MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...

  7. Window服务初级教程以及log4net配置文件初始化

    Window服务初级教程:http://www.jb51.net/article/48987.htm 另外,配置log4net这个日志功能的时候需要初始化,不然会报没有初始化的错误,而且初始化的节点应 ...

  8. 阿里云容器服务--配置自定义路由服务应对DDOS攻击

    阿里云容器服务--配置自定义路由服务应对DDOS攻击 摘要: 容器服务中,除了slb之外,自定义路由服务(基于HAProxy)也可以作为DDOS攻击的一道防线,本文阐述了几种方法来应对普通规模的DDO ...

  9. C# 编写短信发送Window服务

    我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...

随机推荐

  1. js optimization and performance

    http://www.codeproject.com/Articles/551733/Walkthrough-3aplusUsingplustheplusRequireJSplusOpt http:/ ...

  2. Java 8:如何使用流方式查询数据库?

    Speedment 是使用 ORM 方式操作数据库的一种选择,以前我们需要100行操作数据库的 Java 代码,在 Java 8中,可能只需要一行代码. 在90年代末,我使用 Java 开发数据库应用 ...

  3. QiQi and Symmerty

    http://sdu.acmclub.com/index.php?app=problem_title&id=961&problem_id=23772 题意:给出一个01串,问有多少个子 ...

  4. h.264 去块滤波

    块效应及其产生原因 我们在观看视频的时候,在运动剧烈的场景常能观察到图像出现小方块,小方块在边界处呈现不连续的效果(如下图),这种现象被称为块效应(blocking artifact). 首先我们需要 ...

  5. Android实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  6. IOS引用的静态库里包含category文件出现“unrecognized selector”的解决办法

    来自链接:http://blog.csdn.net/ccf0703/article/details/8279187 针对静态库工程中的Category,在被其他工程引入的时候,会出现selector ...

  7. poj 1228 凸包

    题目链接:http://poj.org/problem?id=1228 #include<cstdio> #include<cstring> #include<cmath ...

  8. 杭州(含嘉兴,绍兴,金华,湖州,义乌)Uber优步司机奖励政策(1月25日~1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. hdoj 1276 士兵队列训练问题【模拟】

    士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  10. ImageView类简介

    4.8  图片控件 本节将要介绍的是图片控件ImageView,首先对ImageView类进行简单介绍,然后通过一个案例来说明ImageView的用法. 4.8.1  ImageView类简介 Ima ...