接Window服务(一)

ServiceController方法调用
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
this.AutoLog = true;
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "启动";
WriteLog(state);
ServiceController[] sc = ServiceController.GetDevices();
string str = "";
for (int i = 0; i < sc.Length; i++)
{
WriteLog(sc[i].ServiceName + @"\t" + sc[i].DisplayName + @"\t" + sc[i].Status);
}
WriteLog(" ");
ServiceController[] scArr = ServiceController.GetServices();
for (int i = 0; i < scArr.Length; i++)
{
WriteLog(scArr[i].ServiceName + "\t" + scArr[i].DisplayName + "\t" + scArr[i].Status);
}
ServiceController s = new ServiceController();
s.ServiceName = "Sina";
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "停止";
WriteLog(state);
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
WriteLog(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
}
public void WriteLog(string str)
{
using (StreamWriter sw = File.AppendText(@"c:\service.txt"))
{
sw.WriteLine(str);
sw.Flush();
}
}
}

 1  public partial class Service1 : ServiceBase
2 {
3 public Service1()
4 {
5 InitializeComponent();
6 this.AutoLog = true;
7 }
8 protected override void OnStart(string[] args)
9 {
10 // TODO: 在此处添加代码以启动服务。
11 string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "启动";
12 WriteLog(state);
13 ServiceController[] sc = ServiceController.GetDevices();
14 string str = "";
15 for (int i = 0; i < sc.Length; i++)
16 {
17 WriteLog(sc[i].ServiceName + @"\t" + sc[i].DisplayName + @"\t" + sc[i].Status);
18 }
19 WriteLog(" ");
20 ServiceController[] scArr = ServiceController.GetServices();
21 for (int i = 0; i < scArr.Length; i++)
22 {
23 WriteLog(scArr[i].ServiceName + "\t" + scArr[i].DisplayName + "\t" + scArr[i].Status);
24 }
25 ServiceController s = new ServiceController();
26 s.ServiceName = "Sina";
27 s.Stop();
28 }
29 protected override void OnStop()
30 {
31 // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
32 string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "停止";
33 WriteLog(state);
34 }
35 private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
36 {
37 WriteLog(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
38 }
39 public void WriteLog(string str)
40 {
41 using (StreamWriter sw = File.AppendText(@"c:\service.txt"))
42 {
43 sw.WriteLine(str);
44 sw.Flush();
45 }
46 }
47 }

ServiceController属性

属性   描述    
CanPauseAndContinue 表示服务是否可以停止
CanShutDown 表示服务在系统关闭时是否可以得到通知,CanStop表示服务器是否可以被停止
DependentServices 表示与服务相关联的设备
DisplayName 表示服务控制器所绑定的名称
MachineName 表示服务所在的计算机名称
ServiceName 表示绑定的服务名称
ServiceType 表示控制器所引用的服务类型
ServicesDependedOn   表示服务所依赖ude服务集合Status表示控制器所引用的服务状态

ServiceInstaller类

ServiceInstaller的属性

属性   描述
DisplayName 显示名称
ServiceName 表示服务名称,这个名称必须与Installer将要安装的windows服务名称相同
ServiceDependOn 服务所要用到的其服务名称的一个数组
StartType 表示所安装服务的启动特性,可以为Automatic,Manual或Disable,默认为Manual

ServiceInstaller事件

方法 事件
AfterInstaller() 调用Install方法后发生
AfterRollback() 调用Rollback后发生
AfterUninstall() 调用Uninstall方法后发生
BeforeInstall() 调用Install方法前发生
BeforeRollback() 调用Rollback方法前发生
BeforeUninstall() 调用Uninstall方法前发生
Committed() 调用Commit方法后发生
Committing() 调用Commit方法前发生

ServiceProcessInstaller类

用于安装ServiceBase继承的windows服务,它与一个可执行程序中的所有服务所做基本工作相同

属性

属性 描述
Account 运行服务的当前用户账号  
HelpText   在服务安装选项中给出的帮助信息
Password   运行服务的当前账号密码
UserName   运行服务的当前账号用户名

吊死windows服务

1,日志调试法

2,附加进程断电调试法

步骤:

vs打开MySerivceLog项目

在管理工具-〉服务里面启动MyServiceLog服务

单击vs的调试 -〉附加到进程,如下图

在"可用进程“列表中,选中你要调式的服务的可执行文件名

单击 ”附加“按钮,即可进入调试状态

在timer1_Elapsed方法里设置一个断点,然后等它执行,服务执行到该处时候会自动启动断点。

另外,定时服务可能会定时的访问本域或者非本域的页面,

解决办法用WebClient

1  WebClient client = new WebClient();
2 string uri = "http://127.0.0.1/rss/sina.aspx";
3 byte[] by = client.DownloadData(uri);

window 服务(二)的更多相关文章

  1. 接Window服务(二)

    接Window服务(一) ServiceController方法调用 1 public partial class Service1 : ServiceBase 2 { 3 public Servic ...

  2. Window服务基于Quartz.Net组件实现定时任务调度(二)

    前言: 在上一章中,我们通过利用控制台实现定时任务调度,已经大致了解了如何基于Quartz.Net组件实现任务,至少包括三部分:job(作业),trigger(触发器),scheduler(调度器). ...

  3. WPF Window 服务安装

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

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

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

  5. C#在window服务配置Log4Net.dll

    1.使用背景: C#window服务下添加一个日志记录程序集(Log4Net.dll) 2.添加和使用步骤如下: 一.下载并引入Log4Net.dll程序集到项目中 下载地址:http://loggi ...

  6. C#写的window服务内存溢出

    浅谈c#垃圾回收机制(GC) 写了一个window服务,循环更新sqlite记录,内存一点点稳步增长.三天后,内存溢出.于是,我从自己的代码入手,查找到底哪儿占用内存释放不掉,最终明确是调用servi ...

  7. c# window服务-初学习

    window服务-初学习 一.工具: VS2015+NET Framework4.5. 二.操作: 1.新建windows服务的项目: 2.修改windows服务相关内容: 3.预览windows服务 ...

  8. Redis 下载与配置window服务

    1.Redis下载 Git下载地址:https://github.com/MicrosoftArchive/redis/releases 2.配置Window服务 步骤一:在 Redis目录 输入 c ...

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

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

随机推荐

  1. python的递归算法学习(3):汉诺塔递归算法

    汉诺塔问题是递归函数的经典应用,它来自一个古老传说:在世界刚被创建的时候有一座钻石宝塔A,其上有64个金蝶.所有碟子按从大到小的次序从塔底堆放至塔顶.紧挨着这座塔有另外两个钻石宝塔B和C.从世界创始之 ...

  2. sql:将秒转化成时分秒格式

    DECLARE @a int=20000 SELECT CONVERT(VARCHAR(10),@a/60)+'分'+CONVERT(VARCHAR(10),@a%60)+'秒' --333分20秒 ...

  3. 小白到web前端工程师需要学习哪些知识?

    随着web3.0时代,那么web前端开发技术人才越来越吃香,而且web前端领域划分越来越细,对技术的需求越来越高,想学习web前端的人也是越来越多.那么,如何学习web前端知识?从哪开始?转型成为we ...

  4. 华农oj 2192: hzk又在打人【CRT合并/待补】

    2192: hzk又在打人 Time Limit: 12 Sec Memory Limit: 512 MB Submit: 52 Solved: 1 [Submit][Status][Web Boar ...

  5. springMVC笔记:@ResponseBody

    使用@ResponseBody的方式,将Map形返回值转为json,用作POST请求的返回值.为了解决406 Not Acceptable错误,需要检查以下几项: 1. 依赖包中包含jackson-m ...

  6. 贪心+数学【p3156】 [CQOI2011]分金币 ([HAOI2008]糖果传递)

    题目描述 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. 分析: 设: 每个人最 ...

  7. 本地navicatl连接linux

    首选你Linux服务器上要装配好了MySQL数据库.输入: # mysql -u root -proot mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@ ...

  8. 集合框架(Collection和Collections的区别)

    1.Collection: java.util.Collection 是一个集合接口. 它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类库中有很多具体的实现.Col ...

  9. 【转载】 GNU GCC 选项说明

    GCC 1 Section: GNU Tools (1) Updated: 2003/12/05 Sponsor: GCC Casino Winning Content NAME gcc,g++-GN ...

  10. po_文件格式[转]

    原文: http://cpp.ezbty.org/content/science_doc/po_%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F 摘要:PO 是一种 GNU 定 ...