mirror of
https://github.com/iminet/dev.git
synced 2024-12-21 21:12:39 +01:00
Create Program.cs
This commit is contained in:
parent
920e9fa11e
commit
271e654489
54
csharp/Dotnetservice/Program.cs
Normal file
54
csharp/Dotnetservice/Program.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using System.ServiceProcess
|
||||
|
||||
public static class Program
|
||||
{
|
||||
#region Nested classes to support running as service
|
||||
public const string ServiceName = "MyService";
|
||||
|
||||
public class Service : ServiceBase
|
||||
{
|
||||
public Service()
|
||||
{
|
||||
ServiceName = Program.ServiceName;
|
||||
}
|
||||
|
||||
protected override void OnStart(string[] args)
|
||||
{
|
||||
Program.Start(args);
|
||||
}
|
||||
|
||||
protected override void OnStop()
|
||||
{
|
||||
Program.Stop();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
if (!Environment.UserInteractive)
|
||||
// running as service
|
||||
using (var service = new Service())
|
||||
ServiceBase.Run(service);
|
||||
else
|
||||
{
|
||||
// running as console app
|
||||
Start(args);
|
||||
|
||||
Console.WriteLine("Press any key to stop...");
|
||||
Console.ReadKey(true);
|
||||
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private static void Start(string[] args)
|
||||
{
|
||||
// onstart code here
|
||||
}
|
||||
|
||||
private static void Stop()
|
||||
{
|
||||
// onstop code here
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user