自定义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:\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 服务的更多相关文章
- C# 编写Window服务基础(一)
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
- window 服务(一)
windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...
- C#编写window服务,一步一步(1)
Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...
- WPF Window 服务安装
一.安装服务 1.已管理员的身份启动CMD 2.输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车 3.输入 InstallUtil.exe ...
- SharePoint 2013 中自定义WCF服务
在使用SharePoint2013的时候,如果其他客户端 API 的组合不足,可以通过自定义 Web 服务扩展 SharePoint.默认情况下,SharePoint 2013 不仅支持创建自定义 A ...
- 自定义Attribute 服务端校验 客户端校验
MVC 自定义Attribute 服务端校验 客户端校验/* GitHub stylesheet for MarkdownPad (http://markdownpad.com) *//* Autho ...
- Window服务初级教程以及log4net配置文件初始化
Window服务初级教程:http://www.jb51.net/article/48987.htm 另外,配置log4net这个日志功能的时候需要初始化,不然会报没有初始化的错误,而且初始化的节点应 ...
- 阿里云容器服务--配置自定义路由服务应对DDOS攻击
阿里云容器服务--配置自定义路由服务应对DDOS攻击 摘要: 容器服务中,除了slb之外,自定义路由服务(基于HAProxy)也可以作为DDOS攻击的一道防线,本文阐述了几种方法来应对普通规模的DDO ...
- C# 编写短信发送Window服务
我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...
随机推荐
- .net 开源项目
.NET开发人员值得关注的七个开源项目 [IT168技术分析]微软近几年在.NET社区开源项目方面投入了相当多的时间和资源,不禁让原本对峙的开源社区阵营大吃一惊,从微软.NET社区中的反应来看,微软. ...
- 数据结构(莫队算法):国家集训队2010 小Z的袜子
[题目描述] 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到 ...
- Linux学习笔记19——信号2
上一节中讲到了sigprocmask函数,它的作用是检查或修改它的进程信号掩码,这一节我们主要学习捕捉与忽略信号的函数sigaction和等待信号函数. 一 sigaction函数的作用 定义在接收 ...
- leetcode 字符串分割对称
public class Solution { public List<List<String>> partition(String s) { int len=s.length ...
- SRM 386(1-250pt)
DIV1 250pt 题意:题意很难翻译....其实就是一个暴力...在看到有一个限制条件的范围为1-10的时候就该想到是暴力,我却半天没想到.... tag:brute-force // BEGIN ...
- Win7 64位安装MySQL
1.Win7 64位 安装MySQL5.5版本 安装文件的执行:会提示“已经停止工作”: 2.我下载了mysql-installer-community-5.7.11.0.msi,可以安装成功,中途需 ...
- hdu2817 A sequence of numbers
这题就是判断是等差数列还是等比数列,然后计算结果mod200907 因为数字比较大10的九次方 所以等比用到了快速幂求模 不懂可以看看算法导论,在大数那里有讲 #include <iostrea ...
- jersey 过滤器
这里使用的Jersey 是 1.1 版本 1. web.xml 配置 <?xml version="1.0" encoding="UTF-8"?> ...
- hibernate官方新手教程 (转载)
hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...
- C#和java和android中的NetWorkAdapter,httpRequest,WebView,json,xml
原文地址:http://blog.csdn.net/intbird C#NetWorkAdapter 20121011.======================================== ...