发布:2017/9/9 10:47:38作者:管理员 来源:本站 浏览次数:1934
C# Windows Service服务程序?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 开启服务时要执行的方法
/// </summary>
/// <param name="args"></param>
protected override void OnStart( string [] args)
{
}
/// <summary>
/// 停止服务时要执行的方法
/// </summary>
protected override void OnStop()
{
}
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
//定期类
private System.Timers.Timer aTimer;
/// <summary>
/// 开启服务时要执行的方法
/// </summary>
/// <param name="args"></param>
protected override void OnStart( string [] args)
{
aTimer = new System.Timers.Timer();
//到达时间的时候执行事件;
aTimer.Elapsed += new ElapsedEventHandler(timer1_Tick);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 60 * 1000;
//设置是执行一次(false)还是一直执行(true);
aTimer.AutoReset = true ;
//是否执行System.Timers.Timer.Elapsed事件;
aTimer.Enabled = true ;
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/// <summary>
/// 定时事件
/// </summary>
/// <param name="source">源对象</param>
/// <param name="e">ElapsedEventArgs事件对象</param>
protected void timer1_Tick( object source, ElapsedEventArgs e)
{
//帮助参考[url=http://www.cnblogs.com/sufei/archive/2011/10/22/2221289.html]http://www.cnblogs.com/sufei/archive/2011/10/22/2221289.html[/url]
HttpHelps objhh = new HttpHelps();
//取网站源代码
string result = objhh.GetHttpRequestStringByNUll_Get( "http://sufei.cnblogs.com" , null );
//看看查询出来的网页代码是否存在Perky Su
if (result.Contains( "Perky Su" ))
{
//如果存在我设置的关键字说明我的网站是正常的
WriteFile( "D:\\log\\" + DateTime.Now.ToString( "yyyy-MM-dd" ) + ".txt" ,
"您监控的网站[url=http://sufei.cnblogs.com]http://sufei.cnblogs.com[/url]正常监控时间 " + DateTime.Now.ToString() + "\r\n" );
}
else
{
WriteFile( "D:\\log\\" + DateTime.Now.ToString( "yyyy-MM-dd" ) + ".txt" ,
"您监控的网站[url=http://sufei.cnblogs.com]http://sufei.cnblogs.com[/url]出现异常情况监控时间 " + DateTime.Now.ToString() + "\r\n" );
//如果不存在说明我的网站是不正常的
}
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="_filePath">文件路径</param>
/// <param name="TOEXCELLR">要写入的内容</param>
private void WriteFile( string _filePath, string TOEXCELLR)
{
//检查是否创建文档成功
if (CreateXmlFile(_filePath))
{ //写文本,
using (StreamWriter fs = new StreamWriter(_filePath, true , System.Text.Encoding.UTF8))
{
fs.Write(TOEXCELLR);
}
}
}
/// <summary>
/// 创建文件 的方法
/// </summary>
/// <param name="filepath">路径</param>
/// <returns>文件存在返True否在为False</returns>
private static Boolean CreateXmlFile( string filepath)
{
try
{
//记录成功时的记录
if (!File.Exists(filepath))
{
using (StreamWriter xmlfs = new StreamWriter(filepath, true , System.Text.Encoding.UTF8))
{
xmlfs.Write( "" );
}
return true ;
}
else
{
return true ;
}
}
catch (Exception)
{
return false ;
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
/// <summary>
/// 停止服务时要执行的方法
/// </summary>
protected override void OnStop()
{
aTimer.Enabled = false ;
}
/// <summary>
/// 服务恢复时
/// </summary>
protected override void OnContinue()
{
aTimer.Enabled = true ;
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
[RunInstaller( true )]
public partial class InstallerServices : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null ;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public InstallerServices()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this .spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this .sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this .spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this .spInstaller.Username = null ;
this .spInstaller.Password = null ;
// 设定服务名称服务程序的名称
this .sInstaller.ServiceName = "Jinakong" ;
// 设定服务的启动方式
this .sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this .Installers.AddRange( new System.Configuration.Install.Installer[] { this .spInstaller, this .sInstaller });
}
#endregion
}
|
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4