自定义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服务
我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...
随机推荐
- 【UVALive - 3211】Now or later (二分+2-SAT)
题意: 有n架飞机需要着陆.每架飞机有两种选择,早着陆或者晚着陆,二选其一.现在为了保证飞机的着陆安全,要求两架着陆的飞机的时间间隔的最小值达到最大. 分析: 最小值最大问题我们想到二分答案.对于猜测 ...
- 李洪强漫谈iOS开发[C语言-033]-三元运算符的应用
- Android 自定义dialog(AlertDialog的修改样式)
LayoutInflater inflater = LayoutInflater(AudioActivity.this); View timepickerview = inflater.inflate ...
- lc面试准备:Remove Duplicates from Sorted List II
1 题目 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- .config-20150410
## Automatically generated file; DO NOT EDIT.# OpenWrt Configuration#CONFIG_MODULES=yCONFIG_HAVE_DOT ...
- PuTTY 信息泄露漏洞
漏洞名称: PuTTY 信息泄露漏洞 CNNVD编号: CNNVD-201308-380 发布时间: 2013-08-27 更新时间: 2013-08-27 危害等级: 低危 漏洞类型: 信息泄露 ...
- Node.js权威指南 (2) - Node.js中的交互式运行环境——REPL
2.1 REPL运行环境概述 / 102.2 在REPL运行环境中操作变量 / 102.3 在REPL运行环境中使用下划线字符 / 122.4 在REPL运行环境中直接运行函数 / 122.5 在RE ...
- 动态规划(树形DP):HDU 5886 Tower Defence
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2MAAAERCAIAAAB5Jui9AAAgAElEQVR4nOy9a6wsS3YmFL/cEkh4LP
- HDOJ 2018 母牛的故事
Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...
- VS2010编译libjpeg
环境:win7旗舰版 x64 VS2010 下载源代码下载地址:http://www.ijg.org/,选择windows format file 解压源代码,修改源代码中jconfig.vc为jco ...