最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#添加Windows服務(wù) 定時(shí)任務(wù)

 更新時(shí)間:2017年01月20日 09:22:52   作者:大西瓜3721  
這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#添加Windows服務(wù)的具體方法,供大家參考,具體內(nèi)容如下

源碼下載地址:http://xiazai.jb51.net/201701/yuanma/Windowsservice1(jb51.net).rar

步驟一、創(chuàng)建服務(wù)項(xiàng)目。

步驟二、添加安裝程序。

步驟三、服務(wù)屬性設(shè)置 【serviceInstaller1】。

4.1 添加定時(shí)任務(wù)

public partial class SapSyn : ServiceBase
 {
 System.Timers.Timer timer1; //計(jì)時(shí)器
 System.Timers.Timer timer2; //計(jì)時(shí)器
 System.Timers.Timer timer3; //計(jì)時(shí)器
 System.Timers.Timer timer4; //計(jì)時(shí)器
 public SapSyn()
 {
  InitializeComponent();
 }

 protected override void OnStart(string[] args)
 {
  
  timer1 = new System.Timers.Timer();
  timer1.Interval = 8000; //設(shè)置計(jì)時(shí)器事件間隔執(zhí)行時(shí)間
  timer1.Elapsed += new System.Timers.ElapsedEventHandler(TMStart1_Elapsed);
  timer1.Enabled = true;

  timer2 = new System.Timers.Timer();
  timer2.Interval = 8000; //設(shè)置計(jì)時(shí)器事件間隔執(zhí)行時(shí)間
  timer2.Elapsed += new System.Timers.ElapsedEventHandler(TMStart2_Elapsed);
  timer2.Enabled = true;

  timer3 = new System.Timers.Timer();
  timer3.Interval = 8000; //設(shè)置計(jì)時(shí)器事件間隔執(zhí)行時(shí)間
  timer3.Elapsed += new System.Timers.ElapsedEventHandler(TMStart3_Elapsed);
  timer3.Enabled = true;

  timer4 = new System.Timers.Timer();
  timer4.Interval = 8000; //設(shè)置計(jì)時(shí)器事件間隔執(zhí)行時(shí)間
  timer4.Elapsed += new System.Timers.ElapsedEventHandler(TMStart4_Elapsed);
  timer4.Enabled = true;

 }
 
 protected override void OnStop() //服務(wù)停止執(zhí)行
 {
  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()
 {
  //服務(wù)暫停執(zhí)行代碼
  base.OnPause();
 }
 protected override void OnContinue()
 {
  //服務(wù)恢復(fù)執(zhí)行代碼
  base.OnContinue();
 }
 protected override void OnShutdown()
 {
  //系統(tǒng)即將關(guān)閉執(zhí)行代碼
  base.OnShutdown();
 }

 
 private void TMStart1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 { 
  //執(zhí)行SQL語(yǔ)句或其他操作
  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)
 { 
  //執(zhí)行SQL語(yǔ)句或其他操作
  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)
 { 
  //執(zhí)行SQL語(yǔ)句或其他操作
  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)
 { 
  //執(zhí)行SQL語(yǔ)句或其他操作
  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 設(shè)置服務(wù)啟動(dòng)方式為自動(dòng)啟動(dòng)

[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)
 {
  //參數(shù)為服務(wù)的名字
  System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ServiceSapSyn");
  controller.Start();
 }
 private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
 {

 }
 }

步驟五、腳本配置。

安裝服務(wù)腳本

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

卸載服務(wù)腳本

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

5.1 停止或啟動(dòng)服務(wù)的代碼

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 = "啟動(dòng)服務(wù)";
 }

 /// <summary>
 /// 啟動(dòng)服務(wù)
 /// </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 = "啟動(dòng)服務(wù):你選擇的服務(wù)已經(jīng)啟動(dòng)。";
  Cursor = Cursors.Default;
 }

 /// <summary>
 /// 停止服務(wù)
 /// </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 = "啟動(dòng)服務(wù):你選擇的服務(wù)已經(jīng)卸載。";
  Cursor = Cursors.Default;
 }

 

 private void button3_Click(object sender, EventArgs e)
 {
  ///選擇文件框 對(duì)象
  OpenFileDialog ofd = new OpenFileDialog();
  //打開(kāi)時(shí)指定默認(rèn)路徑
  ofd.InitialDirectory = @"C:\Documents and Settings\Administrator.ICBCOA-6E96E6BE\桌面";
  //如果用戶點(diǎn)擊確定
  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();
 }

 

 //讀寫(xiě)文本 - 寫(xiě)入數(shù)據(jù)按鈕
 private void buttonWrite_Click(string filePath)
 { 
  
 }


 /// <summary>
 /// 運(yùn)行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>
 /// 關(guān)閉進(jìn)程
 /// </summary>
 /// <param name="ProcName">進(jìn)程名稱</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(); // 當(dāng)發(fā)送關(guān)閉窗口命令無(wú)效時(shí)強(qiáng)行結(jié)束進(jìn)程
   result = true;
  }
  }
  return result;
 }

 }

5.2 Form1.Designer.cs 代碼

partial class Form1
 {
 /// <summary>
 /// 必需的設(shè)計(jì)器變量。 Form1.Designer.cs
 /// </summary>
 private System.ComponentModel.IContainer components = null;

 /// <summary>
 /// 清理所有正在使用的資源。
 /// </summary>
 /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
 protected override void Dispose(bool disposing)
 {
  if (disposing && (components != null))
  {
  components.Dispose();
  }
  base.Dispose(disposing);
 }

 #region Windows 窗體設(shè)計(jì)器生成的代碼

 /// <summary>
 /// 設(shè)計(jì)器支持所需的方法 - 不要
 /// 使用代碼編輯器修改此方法的內(nèi)容。
 /// </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 = "啟動(dòng)服務(wù)";
  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 = "停止服務(wù)";
  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 = "請(qǐng)選擇服務(wù)路徑";
  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 = "選擇服務(wù)程序";
  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;
 }

源碼下載地址:http://xiazai.jb51.net/201701/yuanma/Windowsservice1(jb51.net).rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# 泛型字典 Dictionary的使用詳解

    C# 泛型字典 Dictionary的使用詳解

    本文主要介紹了C# 泛型字典 Dictionary的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效

    WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效

    這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 用C#+Selenium+ChromeDriver爬取網(wǎng)頁(yè)(模擬真實(shí)的用戶瀏覽行為)

    用C#+Selenium+ChromeDriver爬取網(wǎng)頁(yè)(模擬真實(shí)的用戶瀏覽行為)

    這篇文章主要介紹了用C#+Selenium+ChromeDriver爬取網(wǎng)頁(yè),模擬真實(shí)的用戶瀏覽行為,需要的小伙伴可以參考一下
    2022-01-01
  • 聊聊C#中的Mixin的具體用法

    聊聊C#中的Mixin的具體用法

    本文主要介紹了C#中的Mixin的具體用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C#微信開(kāi)發(fā)第一章

    C#微信開(kāi)發(fā)第一章

    這篇文章主要為大家詳細(xì)介紹了C#微信開(kāi)發(fā)第一章,很有參考價(jià)值和實(shí)用性,感興趣的小伙伴們可以參考一下
    2016-07-07
  • C#開(kāi)發(fā)Winform實(shí)現(xiàn)文件操作案例

    C#開(kāi)發(fā)Winform實(shí)現(xiàn)文件操作案例

    這篇文章介紹了C#開(kāi)發(fā)Winform實(shí)現(xiàn)文件操作的案例,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法

    C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法,涉及C#操作http的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • c# 單例模式的實(shí)現(xiàn)方法

    c# 單例模式的實(shí)現(xiàn)方法

    這篇文章主要介紹了c# 單例模式的實(shí)現(xiàn)方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • unity實(shí)現(xiàn)延遲回調(diào)工具

    unity實(shí)現(xiàn)延遲回調(diào)工具

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)延遲回調(diào)工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C#常用正則驗(yàn)證函數(shù)示例

    C#常用正則驗(yàn)證函數(shù)示例

    這篇文章主要介紹了C#常用正則驗(yàn)證函數(shù),舉例分析了C#針對(duì)IP驗(yàn)證、價(jià)格驗(yàn)證及正整數(shù)驗(yàn)證的相關(guān)操作技巧,需要的朋友可以參考下
    2017-01-01

最新評(píng)論

广灵县| 福鼎市| 白山市| 大城县| 长海县| 阿坝| 邵阳市| 竹北市| 长春市| 克山县| 静安区| 大兴区| 崇州市| 环江| 马关县| 浦北县| 肇东市| 兴隆县| 金坛市| 苗栗市| 临沭县| 金塔县| 常宁市| 唐山市| 石台县| 文昌市| 奉化市| 长白| 安义县| 东莞市| 克拉玛依市| 巴中市| 松阳县| 永春县| 富顺县| 宁强县| 青浦区| 延寿县| 大庆市| 荆门市| 黎城县|