接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. OutputDebugString方便格式化WIN32封装

    void TRACE(LPCTSTR lpszFmt, ...) { va_list args; va_start(args, lpszFmt); ; TCHAR *lpszBuf = (TCHAR* ...

  2. 本地yum仓库的搭建

    . 1.直接断开网络,模拟生产内网环境 2.将原先的网络yum仓库全部移动到 backup目录下 3.创建本地yum仓库  local_yum.repo vi /etc/yum.repos.d/loc ...

  3. DB2 触发器的写法及表主键结构的修改

    DROP TRIGGER TR_MONTHLYCLOSING; CREATE TRIGGER TR_MONTHLYCLOSING NO CASCADE BEFORE INSERT ON PT_MONT ...

  4. [BZOJ2654]tree 最小生成树+贪心

    2654: tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2435  Solved: 1011[Submit][Status][Discus ...

  5. AC日记——[WC2013]糖果公园 cogs 1817

    [WC2013]糖果公园 思路: 带修改树上莫队(模板): 来,上代码: #include <cmath> #include <cstdio> #include <cst ...

  6. Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析

    装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...

  7. bzoj 3462: DZY Loves Math II

    3462: DZY Loves Math II Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 211  Solved: 103[Submit][Sta ...

  8. Spring Boot特点

    特点: 1.化繁为简,简化配置 2.备受关注,是下一代框架 3.为服务的入门级框架

  9. [Android Traffic] 让android应用在传输网络数据的时候更省电

    到今年6月,我国的手机网民已经达到了3.88亿,超过了电脑终端.相信有智能机的同学都用过手机上网冲浪.但是手机的电量很快被用光了恐怕是每个人都不能忍受的一件事情.而打开数据连接进行网络数据的传输是很耗 ...

  10. hdu1004(c++)

    字符串统计问题,统计每个字符串的次数,输出出现次数最多的字符串 #include<iostream>#include<string>#include<algorithm& ...