Let's take a look at the startup code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;
Namespace windowsservice1
{
Static class Program
{
/// <Summary>
/// 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 );
}
}
}
}
The key lies in the judgment of environment. userinteractive,
Please refer to the above explanation on msdn:
Gets a value that indicates whether the current process is running in user interaction mode.
If the userinteractive attribute is a Windows process or a service (such as IIS) that does not have a user interface at runtime, false is reported. If this attribute is false, do not display the mode dialog box or message box, because there is no graphic user interface used to interact with users.
Http://msdn.microsoft.com/zh-cn/library/system.environment.userinteractive (V = vs.100). aspx
Then let's take a look at the code in service1.cs:
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:
}
}
}
Finally, change the output type of the project.
Right-click the project and click Properties. In the application tag, set output type to console application.
OK. Press F5 to try.