window 服務(二)

接Window服務(一)windows

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類ide

ServiceInstaller的屬性工具

屬性   描述
DisplayName 顯示名稱
ServiceName 表示服務名稱,這個名稱必須與Installer將要安裝的windows服務名稱相同
ServiceDependOn 服務所要用到的其服務名稱的一個數組
StartType 表示所安裝服務的啓動特性,能夠爲Automatic,Manual或Disable,默認爲Manual

 

 

 

 

 

 

ServiceInstaller事件this

方法 事件
AfterInstaller() 調用Install方法後發生
AfterRollback() 調用Rollback後發生
AfterUninstall() 調用Uninstall方法後發生
BeforeInstall() 調用Install方法前發生
BeforeRollback() 調用Rollback方法前發生
BeforeUninstall() 調用Uninstall方法前發生
Committed() 調用Commit方法後發生
Committing() 調用Commit方法前發生

 

 

 

 

 

 

 

 

 

ServiceProcessInstaller類spa

用於安裝ServiceBase繼承的windows服務,它與一個可執行程序中的全部服務所作基本工做相同調試

屬性日誌

屬性 描述
Account 運行服務的當前用戶帳號  
HelpText   在服務安裝選項中給出的幫助信息
Password   運行服務的當前帳號密碼
UserName   運行服務的當前帳號用戶名

 

 

 

 

吊死windows服務code

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);
相關文章
相關標籤/搜索