我正在創建這個簡單的 Windows 服務,每六秒播放一次蜂鳴聲。當我在除錯模式下啟動它時,它會在啟動后立即停止。似乎沒有錯誤或例外。
public partial class Service1 : ServiceBase
{
static Timer myTimer;
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart( null );
}
protected override void OnStart( string[] args )
{
myTimer = new Timer();
myTimer.AutoReset = true;
myTimer.Interval = 60000;
myTimer.Enabled = true;
myTimer.Start();
myTimer.Elapsed = new ElapsedEventHandler( OnTimedEvent );
}
private static void OnTimedEvent( object sender, ElapsedEventArgs e )
{
SystemSounds.Beep.Play();
}
protected override void OnStop()
{
}
}
我的主要方法
static void Main()
{
#if DEBUG
Service1 testService = new Service1();
testService.OnDebug();
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
嗶嗶聲本身就可以正常作業。但它不適用于計時器。
uj5u.com熱心網友回復:
您需要繼續運行您的服務,您可以在行后添加此代碼testService.OnDebug();。它將使您的服務運行,直到您按 q。
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537939.html
標籤:C#定时器窗口服务
上一篇:在C#中將字串轉換為表情符號
下一篇:模式匹配元組與“是”關鍵字?
