【发布时间】:2023-01-20 20:46:03
【问题描述】:
我正在创建这个简单的 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
}
哔哔声本身就可以正常工作。但它不适用于计时器。
【问题讨论】:
-
你能展示你的主要方法吗?
标签: c# timer windows-services