One of the simplest ways to debug Windows services

Source: Internet
Author: 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>//        The main entry point of the application. //        </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);}}}    

  

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;namespacewindowsservice1{ Public Partial classService1:servicebase { PublicService1 () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {             This.        Servicestart (); }        protected Override voidOnStop () { This.        Servicestop (); }        Internal voidDebugstart () { This.        Servicestart (); }        Internal voidDebugstop () { This.        Servicestop (); }        Private voidServicestart () {//TODO:        }        Private voidServicestop () {//TODO:        }    }}

The key is on the judgment of Environment.userinteractive,

Please see the explanations above on MSDN:

Gets a value that indicates whether the current process is running in user interaction mode.

The Userinteractive property reports false for Windows processes that do not have a user interface at run time, or for a service such as IIS . If this property is false, do not display a modal dialog box or a message box because there is no graphical user interface to interact with the user.

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

Then take a look at the code in Service1.cs:

Finally: Change the output type of project

Right-click the project, click Properties, and in the Application tab, change the Output type to console application.

OK, press F5 to try.

Reprinted from Http://www.cnblogs.com/builderman/archive/2011/06/14/2081045.html

One of the simplest ways to debug Windows services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.