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