如果某些服务未被其他服务程序使用,则会自动停止

时间:2014-12-03 09:39:16

标签: c# visual-studio-2012 windows-services

我开发了一个WindowsServices,但是当我尝试运行它时遇到了问题。

这是Windows服务的代码:

using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Threading;

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 
namespace ScriptServices
{

    public partial class Service1 : ServiceBase
    {
        public EventLog eventLog1;
        public System.Timers.Timer timer1;
        private static readonly ILog log = log4net.LogManager.GetLogger
           (MethodBase.GetCurrentMethod().DeclaringType);

        //read the parameter from config file
        static int hours = 0;
        static int minutes = 0;
        //read the parameter 
        static int hoursFrequency = 24;
        static int minutesFrequency = 0;
        static int secondsFrequency = 0;
        static int elapsedTimeBetween2BatchFile = 0;

        public Service1()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("AutomaticallyRunScript"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "AutomaticallyRunScript", "MyNewLog");
            }
            timer1 = new System.Timers.Timer();
            timer1.Interval = 1000; //Intervallo di 10 seconti
            timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
            timer1.Enabled = true;
        }

        /// <summary>
        /// ogni x secondi partirà un thread per andare a sincronizzare il database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer1_Elapsed(object sender, ElapsedEventArgs e)
        {
            //eventLog1.WriteEntry("Thread partiti");
            try
            {
                int i = 0;
                int x = i + 100;
                log.Info("entrato");
                log.Info("i vale: " + i);
            }
            catch (Exception exc)
            {
                log.Error(exc);
                //EventLog.WriteEntry("Errore: "+exc.StackTrace, EventLogEntryType.Error);
            }
        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("GestoreService start");
        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("GestoreService stop");
        }
    }
}

这是主要课程:

using System.Text;
using System.Threading.Tasks;

    namespace ScriptServices
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main()
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }

我尝试通过InstallUtile安装它,它的工作原理。但是,如果我看到TaskManager,我看到服务已停止,所以我尝试手动运行它,然后我收到了这个错误 enter image description here

0 个答案:

没有答案