Запуск службы Windows за каждые 5 минут - PullRequest
1 голос
/ 09 мая 2019

Я создал службу Windows и сделал успешную установку. Я заметил, что, несмотря на то, что установка была выполнена, но служба не отображается в списке services.msc также. Как я могу заставить службу работать каждые 5 минут.

Отработано до части установки сервиса.

       public partial class FeedbcakSMSToDCService : ServiceBase
    {
        DBBase obj = new DBBase();

        Thread threadPRSender;
        string fileName = "D:\\FeedbackSMSlog.txt";
        System.Timers.Timer timer = new System.Timers.Timer();
        System.Timers.Timer TimerObj = new System.Timers.Timer();
        FeedBackSMSToDCHandler feedbackSmstoDcHandler = null;
        EventLog m_EventLog = new EventLog();
        ThreadStart threadSender = null;
        FeedBackSMSToDCHandler FB = new FeedBackSMSToDCHandler();
        public FeedbcakSMSToDCService()
        {
            InitializeComponent();
            feedbackSmstoDcHandler = new FeedBackSMSToDCHandler();
            threadSender = new ThreadStart(ProcessSMSToDC);
            threadPRSender = new Thread(threadSender);

        }

        static void Main()
        {

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new FeedbcakSMSToDCService()
            };
            ServiceBase.Run(ServicesToRun);

        }
        protected override void OnStart(string[] args)
        {
            try
            {

                System.Diagnostics.Debugger.Launch();
                if (File.Exists(fileName))
                {
                    File.AppendAllText(@"D:\FeedbackSMSlog.txt", "Service started on : " + DateTime.Now.ToString() + Environment.NewLine);
                }
                else
                {
                   FileStream fs= File.Create(fileName);
                    fs.Close();
                    File.AppendAllText(@"D:\FeedbackSMSlog.txt", "Service started on : " + DateTime.Now.ToString() + Environment.NewLine);
                }
                //FB.ProcessSMS();
                //SetTimeInterval();
                //timer.Interval =Convert.ToDouble(ConfigurationManager.AppSettings["TimerInterval"].ToString());
                timer = new System.Timers.Timer(5 * 60 * 1000);
                timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
                timer.Start();
            }

            catch (Exception ex)
            {
                ex.Source = "On Service Start";

                OASIS.Utility.ExceptionManager.HandleException(ex);
            }
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();
            FB.ProcessSMS();
            timer.Start();

        }

        protected override void OnStop()
        {
            File.AppendAllText(@"D:\FeedbackSMSlog.txt", "Service stopped on : " + DateTime.Now.ToString() + Environment.NewLine);

            //TimerObj.Enabled = false;
           timer.Stop();
           threadPRSender = null;
        }
        private void timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {


                timer.Stop();
                if (threadPRSender.ThreadState == System.Threading.ThreadState.Running)
                {
                    return;
                }
                if (threadPRSender.ThreadState == System.Threading.ThreadState.Unstarted
                    || threadPRSender.ThreadState == System.Threading.ThreadState.Stopped
                    || threadPRSender.ThreadState == System.Threading.ThreadState.Aborted
                    || !threadPRSender.IsAlive)
                {

                    threadSender = new ThreadStart(ProcessSMSToDC);
                    threadPRSender = new Thread(threadSender);
                    threadPRSender.Start();
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"C:\ERP\Live\Background Service\Calendar\AttendanceLog.txt", "timer_Tick error : " + ex.StackTrace.ToString() + Environment.NewLine);
                ex.Source = "Timer Tick error";
                OASIS.Utility.ExceptionManager.HandleException(ex);
            }
        }



        public void ProcessSMSToDC()
        {
            try
            {
                feedbackSmstoDcHandler = new FeedBackSMSToDCHandler();
                feedbackSmstoDcHandler.ProcessSMS();

            }
            catch (Exception ex)
            {
                ex.Source = "SMS Sent To DC";
                OASIS.Utility.ExceptionManager.HandleException(ex);
                File.AppendAllText(@"C:\ERP\Live\Background Service\Feedback\SmsSendToDC.txt", "Error while ProcessSMS : " + ex.StackTrace.ToString() + Environment.NewLine);
            }
        }
        private void SetTimeInterval()
        {
            try
            {
                timer.Start();                
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"C:\ERP\Live\Background Service\Feedback\SMSToDCLog.txt", "Error while setting time interval : " + ex.StackTrace.ToString() + Environment.NewLine);
            }
        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...