調試windows服務最簡單的方法之一

來源:互聯網
上載者:User

標籤:

http://item.jd.com/733388.html

using System;using System.Collections.Generic;using System.Linq;using System.ServiceProcess;using System.Text;namespace WindowsService1{    static class Program    {        /// <summary>        /// 應用程式的主進入點。        /// </summary>        static void Main()        {            Service1 s = new Service1();            if (Environment.UserInteractive)            {                s.DebugStart();                Console.ReadKey();                s.DebugStop();            }            else            {                ServiceBase[] ServicesToRun;                ServicesToRun = new ServiceBase[] { s };                ServiceBase.Run(ServicesToRun);            }        }    }}

  

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 WindowsService1{    public partial class Service1 : ServiceBase    {        public Service1()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            this.ServiceStart();        }        protected override void OnStop()        {            this.ServiceStop();        }        internal void DebugStart()        {            this.ServiceStart();        }        internal void DebugStop()        {            this.ServiceStop();        }        private void ServiceStart()        {            // TODO:        }        private void ServiceStop()        {            // TODO:        }    }}

關鍵就在對Environment.UserInteractive的判斷上,

請看MSDN上面的解釋:

擷取一個值,用以指示當前進程是否在使用者互動模式中運行。

UserInteractive 屬性為運行時無使用者介面的 Windows 進程或一個服務(如 IIS)報告 false。 如果此屬性為 false,請不要顯示模式對話方塊或訊息框,因為沒有用來與使用者進行互動的圖形化使用者介面。

http://msdn.microsoft.com/zh-cn/library/system.environment.userinteractive(v=VS.100).aspx

然後看一下Service1.cs中的代碼:

 

最後:更改Project的輸出類型

右擊該Project,點擊Properties,在Application標籤中,更改Output Type為Console Application。

OK啦,按F5試試

 

轉載自http://www.cnblogs.com/builderman/archive/2011/06/14/2081045.html

調試windows服務最簡單的方法之一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.