c# - The C# service running in Windows 8 but is not running in Windows 7
问题描述
I have a problem about C# service.
I wrote an audible alarm service for a SCADA software. I created an installation file for this service. I have installed this service both on a PC with Windows 8 and on a PC with Windows 7 installed. The service works fine in Windows 8 but not in Windows 7. I don't get any errors when I load it. I created a console application using the same codes on the PC with Windows 7 installed. It works fine when I run the console application. Does anyone have any idea? There's probably something I'm missing out on. You can find the codes below.
using System;
using System.ServiceProcess;
using System.Media;
using System.IO;
using System.Timers;
namespace MS_audible_alarm
{
public partial class Service1 : ServiceBase
{
public static Timer timer;
public static int counter;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer = new Timer();
timer.Interval = 5000;
timer.Elapsed += new ElapsedEventHandler(audible_alarm);
timer.Start();
counter = 0;
}
protected override void OnStop()
{
timer.Close();
}
private static void audible_alarm(Object source, ElapsedEventArgs e)
{
SoundPlayer sp = new SoundPlayer(@"C:\sc\apl\YNY\PICT\DataBase\Alarm_Sesi.wav");
string fileAddress = @"C:\sc\apl\YNY\PICT\DataBase\audible_alarm.txt";
FileStream fs = new FileStream(fileAddress, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string alarmDurumu = sr.ReadLine();
if (alarmDurumu == "Alarm" && counter == 0)
{
sp.Play();
counter = 1;
}
else if (alarmDurumu == "Normal" && counter == 1)
{
sp.Stop();
counter = 0;
}
sr.Close();
fs.Close();
}
}
}
Thanks for help.
Best Regards.
解决方案
推荐阅读
- javascript - 如何使用 API ajax 调用实现动态表并将数据填充到数据表中?
- json - 如何展开 JSON 对象?
- c# - Switch 语句包含具有相同标签值的多个案例,但不包含
- java - Java关于反序列化继承类的一些问题
- c# - 从 Serilog 将事件摄取到 Seq 中的问题
- javascript - 代码:'MODULE_NOT_FOUND',requireStack[] node.js
- r - R:按行替换值
- reactjs - 我打电话给网络一次,但在测试中它被抓了两次
- php - 从后到前发送错误消息(php + html)
- php - 如何用pdo根据store一一拉取