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

VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法

 更新時(shí)間:2017年02月15日 11:49:33   作者:Pete-Jones  
這篇文章主要介紹了VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法,需要的朋友可以參考下

1、創(chuàng)建Windows服務(wù)

 

說明:

a)Description 服務(wù)描述,直接顯示到Windows服務(wù)列表中的描述;

b)DisplayName 服務(wù)顯示名稱,直接顯示到Windows服務(wù)列表中的名稱;

c)ServiceName 服務(wù)進(jìn)程名稱,安裝與卸載服務(wù)時(shí)的唯一標(biāo)識(shí)。

單擊“serviceProcessInstaller1”,在其屬性窗口中設(shè)置Account帳號(hào)方式,建議為LocalService(當(dāng)然也可以Account屬性改為 LocalSystem,這樣,不論是以哪個(gè)用戶登錄的系統(tǒng),服務(wù)總會(huì)啟動(dòng))。

編寫安裝和卸載腳本,并將放在bin/debug或bin/Release文件夾下。

安裝腳本

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe %~dp0exe程序的名稱.exe
Net Start 服務(wù)名稱
sc config 服務(wù)名稱 start= auto
pause

這里注意,在exe程序的名稱前面有 %~dp0 這是代表當(dāng)前位置

服務(wù)名稱 對(duì)應(yīng) 上面我們創(chuàng)建服務(wù)時(shí)ServerName的名稱

卸載腳本

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u %~dp0exe程序的名稱.exe
pause

同時(shí)還要注意一下,本人用的.NET4.0的版本,所以\Microsoft.NET\Framework\v4.0.30319\installutil.exe 這一段要根據(jù)你機(jī)器安裝.NET的版本來定。

其實(shí)腳本主要是通過installutil.exe 來進(jìn)行安裝和卸載服務(wù)的,同時(shí)此處涉及的批處理命令不多。

2、調(diào)試windows服務(wù)

在項(xiàng)目中不用啟動(dòng)windows服務(wù)項(xiàng)目,而是直接附加進(jìn)程來進(jìn)行調(diào)試。

 

在可用進(jìn)程中,查找到你剛才通過腳本安裝的服務(wù)就可以了。

再發(fā)一個(gè)寫入服務(wù)代碼的Demo

public partial class MMSServer : ServiceBase
  {
    private Timer time = new Timer();
    public MMSServer()
    {
      InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
#if DEBUG
      if (!Debugger.IsAttached)
        Debugger.Launch();
      Debugger.Break();
#endif
      WriteLog("服務(wù)啟動(dòng),時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
      time.Elapsed += new ElapsedEventHandler(MethodEvent);
      time.Interval = 3 * 1000;
      time.Start();
    }

    protected override void OnPause()
    {
#if DEBUG
      if (!Debugger.IsAttached)
        Debugger.Launch();
      Debugger.Break();
#endif
      WriteLog("服務(wù)暫停,時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
      base.OnPause();
    }
    protected override void OnContinue()
    {
#if DEBUG
      if (!Debugger.IsAttached)
        Debugger.Launch();
      Debugger.Break();
#endif
      WriteLog("服務(wù)恢復(fù),時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
      base.OnContinue();
    }

    protected override void OnShutdown()
    {
      WriteLog("計(jì)算機(jī)關(guān)閉,時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
      base.OnShutdown();
    }

    private void MethodEvent(object source, System.Timers.ElapsedEventArgs e)
    {
      time.Enabled = false;
      string result = string.Empty;
      try
      {
        //.........
        result = "執(zhí)行成功,時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n";
      }
      catch (Exception ex)
      {
        result = "執(zhí)行失敗,原因:" + ex.Message + "\r\n";
      }
      finally
      {
        WriteLog(result);
        time.Enabled = true;
      }
    }
    protected override void OnStop()
    {
#if DEBUG
      if (!Debugger.IsAttached)
        Debugger.Launch();
      Debugger.Break();
#endif
      WriteLog("服務(wù)停止,時(shí)間:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
    }
    /// <summary>
    /// 日志記錄
    /// </summary>
    /// <param name="logInfo"></param>
    private void WriteLog(string logInfo)
    {
      try
      {
        string logDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
        if (!Directory.Exists(logDirectory))
        {
          Directory.CreateDirectory(logDirectory);
        }
        string filePath = logDirectory + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
        File.AppendAllText(filePath, logInfo);
      }
      catch
      {

      }
    }
  }

以上就是關(guān)于VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的全部內(nèi)容了,希望大家以后多多支持腳本之家

相關(guān)文章

最新評(píng)論

景洪市| 县级市| 凌云县| 蕲春县| 江孜县| 布尔津县| 芷江| 松溪县| 千阳县| 辰溪县| 长泰县| 缙云县| 五莲县| 建阳市| 石泉县| 金寨县| 酒泉市| 湘阴县| 任丘市| 儋州市| 兴城市| 永州市| 平陆县| 济宁市| 西林县| 枣强县| 高台县| 萝北县| 临桂县| 石景山区| 云浮市| 拜泉县| 民权县| 罗城| 辽阳市| 苗栗市| 浑源县| 定襄县| 安远县| 临高县| 大新县|