首页 > 解决方案 > 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.

标签: c#service

解决方案



推荐阅读