亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#添加Windows服務 定時任務

2019-10-29 21:15:37
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C#添加Windows服務的具體方法,供大家參考,具體內容如下

步驟一、創建服務項目。

C#,Windows,定時任務

步驟二、添加安裝程序。

C#,Windows,定時任務

步驟三、服務屬性設置 【serviceInstaller1】。

C#,Windows,定時任務

4.1 添加定時任務

public partial class SapSyn : ServiceBase { System.Timers.Timer timer1; //計時器 System.Timers.Timer timer2; //計時器 System.Timers.Timer timer3; //計時器 System.Timers.Timer timer4; //計時器 public SapSyn() {  InitializeComponent(); } protected override void OnStart(string[] args) {    timer1 = new System.Timers.Timer();  timer1.Interval = 8000; //設置計時器事件間隔執行時間  timer1.Elapsed += new System.Timers.ElapsedEventHandler(TMStart1_Elapsed);  timer1.Enabled = true;  timer2 = new System.Timers.Timer();  timer2.Interval = 8000; //設置計時器事件間隔執行時間  timer2.Elapsed += new System.Timers.ElapsedEventHandler(TMStart2_Elapsed);  timer2.Enabled = true;  timer3 = new System.Timers.Timer();  timer3.Interval = 8000; //設置計時器事件間隔執行時間  timer3.Elapsed += new System.Timers.ElapsedEventHandler(TMStart3_Elapsed);  timer3.Enabled = true;  timer4 = new System.Timers.Timer();  timer4.Interval = 8000; //設置計時器事件間隔執行時間  timer4.Elapsed += new System.Timers.ElapsedEventHandler(TMStart4_Elapsed);  timer4.Enabled = true; }  protected override void OnStop() //服務停止執行 {  using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C://log.txt", true))  {  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");  }  this.timer1.Enabled = false;  this.timer2.Enabled = false;  this.timer3.Enabled = false;    this.timer4.Enabled = false; } protected override void OnPause() {  //服務暫停執行代碼  base.OnPause(); } protected override void OnContinue() {  //服務恢復執行代碼  base.OnContinue(); } protected override void OnShutdown() {  //系統即將關閉執行代碼  base.OnShutdown(); }  private void TMStart1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {   //執行SQL語句或其他操作  using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C://" + 1 + "log.txt", true))  {  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");  } } private void TMStart2_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {   //執行SQL語句或其他操作  using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C://" + 2 + "log.txt", true))  {  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");  } } private void TMStart3_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {   //執行SQL語句或其他操作  using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C://" + 3 + "log.txt", true))  {  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");  } } private void TMStart4_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {   //執行SQL語句或其他操作  using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C://" + 4 + "log.txt", true))  {  sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");  } } }

4.2 設置服務啟動方式為自動啟動

[RunInstaller(true)] public partial class ProjectInstaller : System.Configuration.Install.Installer {  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("ServiceSapSyn");  controller.Start(); } private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) { } }

步驟五、腳本配置。

安裝服務腳本

 

復制代碼 代碼如下:
%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe WindowsServiceTest.exeNet Start ServiceTestsc config ServiceTest start= auto

 

卸載服務腳本

 

復制代碼 代碼如下:
%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe /u WindowsServiceTest.exe

 

C#,Windows,定時任務

5.1 停止或啟動服務的代碼

public partial class Form1 : Form { public Form1() {  InitializeComponent(); }  public string thispath = Application.StartupPath;  public string Propath = "";  private void Form1_Load(object sender, EventArgs e) {  this.Text = "啟動服務"; } /// <summary> /// 啟動服務 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) {  Cursor = Cursors.WaitCursor;  string StarPath = @"%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe " + Propath;  FileStream fs = new FileStream(thispath + "//Install.bat", FileMode.Create);  StreamWriter sw = new StreamWriter(fs);  try  {  sw.WriteLine(StarPath);  sw.WriteLine("Net Start ServiceTest");  sw.WriteLine("sc config ServiceTest start= auto");  }  catch (Exception ex)  {  MessageBox.Show(ex.Message.ToString());  }  finally  {  sw.Close();  fs.Close();  }  System.Diagnostics.Process.Start(thispath + "//Install.bat");  this.Text = "啟動服務:你選擇的服務已經啟動。";  Cursor = Cursors.Default; } /// <summary> /// 停止服務 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) {  Cursor = Cursors.WaitCursor;  string StarPath = @"%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil.exe /u " + Propath;  FileStream fs = new FileStream(thispath + "//Uninstall.bat", FileMode.Create);  StreamWriter sw = new StreamWriter(fs);  try  {  sw.WriteLine(StarPath);   }  catch (Exception ex)  {  MessageBox.Show(ex.Message.ToString());  }  finally  {  sw.Close();  fs.Close();  }  System.Diagnostics.Process.Start(thispath + "//Uninstall.bat");  this.Text = "啟動服務:你選擇的服務已經卸載。";  Cursor = Cursors.Default; }  private void button3_Click(object sender, EventArgs e) {  ///選擇文件框 對象  OpenFileDialog ofd = new OpenFileDialog();  //打開時指定默認路徑  ofd.InitialDirectory = @"C:/Documents and Settings/Administrator.ICBCOA-6E96E6BE/桌面";  //如果用戶點擊確定  if (ofd.ShowDialog() == DialogResult.OK)  {  //將用戶選擇的文件路徑 顯示 在文本框中  textBox1.Text = ofd.FileName;  Propath = textBox1.Text;  }  if (File.Exists(thispath + "//Uninstall.bat"))  {  File.Delete(thispath + "//Uninstall.bat");  }  File.Create(thispath + "//Uninstall.bat").Close();  if (File.Exists(thispath + "//Install.bat"))  {  File.Delete(thispath + "//Install.bat");  }  File.Create(thispath + "//Install.bat").Close(); }  //讀寫文本 - 寫入數據按鈕 private void buttonWrite_Click(string filePath) {    } /// <summary> /// 運行CMD命令 /// </summary> /// <param name="cmd">命令</param> /// <returns></returns> public static string Cmd(string[] cmd) {  Process p = new Process();  p.StartInfo.FileName = "cmd.exe";  p.StartInfo.UseShellExecute = false;  p.StartInfo.RedirectStandardInput = true;  p.StartInfo.RedirectStandardOutput = true;  p.StartInfo.RedirectStandardError = true;  p.StartInfo.CreateNoWindow = true;  p.Start();  p.StandardInput.AutoFlush = true;  for (int i = 0; i < cmd.Length; i++)  {  p.StandardInput.WriteLine(cmd[i].ToString());  }  p.StandardInput.WriteLine("exit");  string strRst = p.StandardOutput.ReadToEnd();  p.WaitForExit();  p.Close();  return strRst; } /// <summary> /// 關閉進程 /// </summary> /// <param name="ProcName">進程名稱</param> /// <returns></returns> public static bool CloseProcess(string ProcName) {  bool result = false;  System.Collections.ArrayList procList = new System.Collections.ArrayList();  string tempName = "";  int begpos;  int endpos;  foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())  {  tempName = thisProc.ToString();  begpos = tempName.IndexOf("(") + 1;  endpos = tempName.IndexOf(")");  tempName = tempName.Substring(begpos, endpos - begpos);  procList.Add(tempName);  if (tempName == ProcName)  {   if (!thisProc.CloseMainWindow())   thisProc.Kill(); // 當發送關閉窗口命令無效時強行結束進程   result = true;  }  }  return result; } }

5.2 Form1.Designer.cs 代碼

partial class Form1 { /// <summary> /// 必需的設計器變量。 Form1.Designer.cs /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) {  if (disposing && (components != null))  {  components.Dispose();  }  base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() {  System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));  this.button1 = new System.Windows.Forms.Button();  this.button2 = new System.Windows.Forms.Button();  this.textBox1 = new System.Windows.Forms.TextBox();  this.button3 = new System.Windows.Forms.Button();  this.SuspendLayout();  //   // button1  //   this.button1.Font = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  this.button1.Location = new System.Drawing.Point(12, 90);  this.button1.Name = "button1";  this.button1.Size = new System.Drawing.Size(134, 60);  this.button1.TabIndex = 0;  this.button1.Text = "啟動服務";  this.button1.UseVisualStyleBackColor = true;  this.button1.Click += new System.EventHandler(this.button1_Click);  //   // button2  //   this.button2.Font = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  this.button2.Location = new System.Drawing.Point(280, 90);  this.button2.Name = "button2";  this.button2.Size = new System.Drawing.Size(134, 60);  this.button2.TabIndex = 0;  this.button2.Text = "停止服務";  this.button2.UseVisualStyleBackColor = true;  this.button2.Click += new System.EventHandler(this.button2_Click);  //   // textBox1  //   this.textBox1.Font = new System.Drawing.Font("微軟雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  this.textBox1.ForeColor = System.Drawing.Color.Maroon;  this.textBox1.Location = new System.Drawing.Point(108, 13);  this.textBox1.Multiline = true;  this.textBox1.Name = "textBox1";  this.textBox1.Size = new System.Drawing.Size(306, 67);  this.textBox1.TabIndex = 2;  //   // button3  //   this.button3.Font = new System.Drawing.Font("微軟雅黑", 10.5F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  this.button3.ForeColor = System.Drawing.Color.Blue;  this.button3.Location = new System.Drawing.Point(12, 12);  this.button3.Name = "button3";  this.button3.Size = new System.Drawing.Size(90, 68);  this.button3.TabIndex = 3;  this.button3.Text = "請選擇服務路徑";  this.button3.UseVisualStyleBackColor = true;  this.button3.Click += new System.EventHandler(this.button3_Click);  //   // Form1  //   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  this.ClientSize = new System.Drawing.Size(419, 155);  this.Controls.Add(this.button3);  this.Controls.Add(this.textBox1);  this.Controls.Add(this.button2);  this.Controls.Add(this.button1);  this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));  this.Name = "Form1";  this.Text = "選擇服務程序";  this.Load += new System.EventHandler(this.Form1_Load);  this.ResumeLayout(false);  this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button3; }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
一本大道久久加勒比香蕉| 欧美巨大黑人极品精男| 欧美性jizz18性欧美| 亚洲美女视频网站| 亚洲视频在线免费看| 欧美性猛交xxxx黑人猛交| 中文字幕视频在线免费欧美日韩综合在线看| 日韩av影院在线观看| 亚洲欧美日韩高清| 亚洲国产精彩中文乱码av在线播放| 久久久久久国产精品美女| www.亚洲一二| 精品久久久久久| 亚洲大胆人体在线| 国产精品成人观看视频国产奇米| 日韩亚洲成人av在线| 91精品国产一区| 亚洲男女自偷自拍图片另类| 亚洲欧美日韩在线一区| 日韩欧美国产一区二区| 国产99久久精品一区二区永久免费| 欧美中文在线视频| 日韩男女性生活视频| 日韩精品在线电影| 在线播放国产精品| 国产一区二区三区18| 亚洲天堂一区二区三区| 中文在线不卡视频| 欧美激情免费在线| 中文字幕亚洲欧美在线| 91免费观看网站| 国产精品国语对白| 91精品国产91久久久久久最新| 97在线视频免费播放| 中文字幕av一区中文字幕天堂| 精品久久久久人成| 久久不射热爱视频精品| 久热精品视频在线免费观看| 国产午夜精品久久久| 国产精选久久久久久| 亚洲欧美999| 国产精品网站大全| 日本19禁啪啪免费观看www| 亚洲女人被黑人巨大进入al| 日韩av免费在线| 久久精品99久久久香蕉| 日韩美女主播视频| 色yeye香蕉凹凸一区二区av| 国产精品成人在线| 国产欧美日韩视频| 国产男人精品视频| 青青久久av北条麻妃黑人| 97热在线精品视频在线观看| 国产视频久久久久| 亚洲成人黄色网址| 久久久亚洲影院| 中文在线资源观看视频网站免费不卡| 91精品国产91久久久久| 久久精品一本久久99精品| 欧美午夜激情在线| 欧美丝袜美女中出在线| 久久久久久久久久婷婷| 奇门遁甲1982国语版免费观看高清| 欧美专区第一页| 一本一本久久a久久精品牛牛影视| 国产成人精品久久久| 国内精品小视频在线观看| 亚洲一区二区三区毛片| 欧美成人激情图片网| 久久韩国免费视频| 国产成人精品视| 国产综合在线观看视频| 九九精品在线播放| 中文欧美在线视频| 亚洲一区二区三区乱码aⅴ| 深夜精品寂寞黄网站在线观看| 国产精品久久久久久久久久三级| 亚洲va久久久噜噜噜久久天堂| 日韩欧美aⅴ综合网站发布| 91精品一区二区| 热99精品里视频精品| 国产丝袜一区二区三区免费视频| 中日韩午夜理伦电影免费| 在线观看日韩av| 久久精品在线播放| 日韩在线视频观看正片免费网站| 狠狠躁夜夜躁人人爽超碰91| 午夜精品福利电影| 亚洲精品短视频| 久久精品免费播放| 国产99久久精品一区二区 夜夜躁日日躁| 国产一区二区三区高清在线观看| 亚洲欧美综合区自拍另类| 国产精品视频区| 亚洲精品综合精品自拍| 免费97视频在线精品国自产拍| 91在线视频九色| 精品美女永久免费视频| 亚洲精品小视频| 欧美一级片免费在线| 欧美韩日一区二区| 亚洲已满18点击进入在线看片| 91热福利电影| 国产精品揄拍500视频| 成人网在线免费观看| 亚洲欧美一区二区三区久久| 麻豆一区二区在线观看| 亚洲国产精品国自产拍av秋霞| 久久精品视频在线播放| 亚洲第一综合天堂另类专| 亚洲自拍偷拍视频| 久久亚洲精品中文字幕冲田杏梨| 97婷婷涩涩精品一区| 一区二区欧美激情| 国产精品旅馆在线| 欧美成人午夜剧场免费观看| 精品国产乱码久久久久久婷婷| 国产精品久久久久久搜索| 中文字幕久久久av一区| 久久久久久久久91| 国产美女精品视频免费观看| 亚洲欧美变态国产另类| 国产乱肥老妇国产一区二| 中文.日本.精品| 欧美成人久久久| 国产精品自拍小视频| 久久手机精品视频| 中文字幕成人精品久久不卡| 国产精品久久久久久av| 久久精品视频导航| 中文字幕欧美日韩在线| 国产精品久久久久久久久久小说| 最近中文字幕mv在线一区二区三区四区| 国产成人在线一区| 午夜欧美大片免费观看| 亚洲视频在线观看免费| 日本午夜精品理论片a级appf发布| 性欧美亚洲xxxx乳在线观看| 日韩精品免费视频| 秋霞成人午夜鲁丝一区二区三区| 色噜噜久久综合伊人一本| 精品国产91久久久久久老师| 亚洲国产精品成人精品| 日韩在线欧美在线国产在线| 538国产精品视频一区二区| 欧美色视频日本高清在线观看| 久久久www成人免费精品张筱雨| 久久久久久久久国产| 国产精品69av| www.精品av.com| 97国产精品视频人人做人人爱| 国产在线日韩在线| 久久视频中文字幕| 国产精品视频地址| 4388成人网| 亚洲男人天堂九九视频| 欧洲成人性视频| 欧美在线性视频| 亚洲xxxxx电影| 亚洲精品suv精品一区二区| 久久亚洲春色中文字幕| 亚洲欧美日韩爽爽影院| 亚洲另类图片色| 亚洲国产精品va在线|