104 lines
3.8 KiB
C#
104 lines
3.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.ServiceProcess;
|
|
|
|
namespace AutoRunService
|
|
{
|
|
|
|
[Microsoft.VisualBasic.CompilerServices.DesignerGenerated()]
|
|
public partial class AutoRunService : ServiceBase
|
|
{
|
|
|
|
// UserService 覆寫 Dispose 以清除元件清單。
|
|
[DebuggerNonUserCode()]
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
try
|
|
{
|
|
if (disposing && components != null)
|
|
{
|
|
components.Dispose();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
|
|
// 處理序的主要進入點
|
|
[MTAThread()]
|
|
[DebuggerNonUserCode()]
|
|
public static void Main()
|
|
{
|
|
ServiceBase[] ServicesToRun;
|
|
|
|
// 在同一個處理序中可以執行多個 NT 服務。若要在這個處理序中
|
|
// 加入另一項服務,請修改下行程式碼,
|
|
// 以建立第二個服務物件。例如,
|
|
//
|
|
// ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
|
|
//
|
|
ServicesToRun = new ServiceBase[] { new AutoRunService() };
|
|
//RunInteractive(ServicesToRun); //debug
|
|
Run(ServicesToRun);
|
|
}
|
|
private static void RunInteractive(ServiceBase[] servicesToRun)
|
|
{
|
|
var onStartMethod = typeof(ServiceBase).GetMethod("OnStart", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
|
|
|
foreach (ServiceBase service in servicesToRun)
|
|
{
|
|
Console.Write("Starting {0}...", service.ServiceName);
|
|
onStartMethod.Invoke(service, new object[] { new string[] { } });
|
|
Console.Write("Started");
|
|
}
|
|
|
|
Console.WriteLine("Press any key to stop the services");
|
|
Console.ReadKey();
|
|
var onStopMethod = typeof(ServiceBase).GetMethod("OnStop", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
|
|
|
|
foreach (ServiceBase service in servicesToRun)
|
|
{
|
|
Console.Write("Stopping {0}...", service.ServiceName);
|
|
onStopMethod.Invoke(service, null);
|
|
Console.WriteLine("Stopped");
|
|
}
|
|
}
|
|
|
|
// 為元件設計工具的必要項
|
|
private System.ComponentModel.IContainer components;
|
|
|
|
// 注意: 以下為元件設計工具的所需的程序
|
|
// 您可以使用元件設計工具進行修改。
|
|
// 請不要使用程式碼編輯器進行修改。
|
|
[DebuggerStepThrough()]
|
|
private void InitializeComponent()
|
|
{
|
|
TimerCheckJobList = new System.Timers.Timer();
|
|
TimerCheckJobList.Elapsed += new System.Timers.ElapsedEventHandler(TimerCheckJobList_Elapsed);
|
|
TimerCheckQueue = new System.Timers.Timer();
|
|
TimerCheckQueue.Elapsed += new System.Timers.ElapsedEventHandler(TimerCheckQueue_Elapsed);
|
|
((System.ComponentModel.ISupportInitialize)TimerCheckJobList).BeginInit();
|
|
((System.ComponentModel.ISupportInitialize)TimerCheckQueue).BeginInit();
|
|
//
|
|
// TimerCheckJobList
|
|
//
|
|
TimerCheckJobList.Interval = 300000.0d;
|
|
//
|
|
// TimerCheckQueue
|
|
//
|
|
TimerCheckQueue.Interval = 60000.0d;
|
|
//
|
|
// AutoRunService
|
|
//
|
|
ServiceName = "MES AutoRun Service";
|
|
((System.ComponentModel.ISupportInitialize)TimerCheckJobList).EndInit();
|
|
((System.ComponentModel.ISupportInitialize)TimerCheckQueue).EndInit();
|
|
|
|
}
|
|
internal System.Timers.Timer TimerCheckJobList;
|
|
internal System.Timers.Timer TimerCheckQueue;
|
|
|
|
}
|
|
} |