【问题标题】:How can I figured out why the timer event in my Windows Service isn't firing?我怎样才能弄清楚为什么我的 Windows 服务中的计时器事件没有触发?
【发布时间】:2017-01-07 01:47:32
【问题描述】:

我有一个非常基本的服务

using System.ServiceProcess;
using System.Timers;
using SystemTimer = System.Timers.Timer;
using System.Net;
using System.Data.SqlClient;
using System;

namespace ServiceThatConnectsToADb
{
    public class LrcArchiver : ServiceBase
    {
        private static SystemTimer PageLoadTimer = new SystemTimer(5000);

        public LrcArchiver()
        {
            ServiceName = "SO Archiver";
            CanStop = true;
            CanPauseAndContinue = true;
            AutoLog = true;
            PageLoadTimer.Elapsed += new ElapsedEventHandler(WritePageToDb);
        }

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry(ServiceName + " started");
        }

        protected override void OnStop()
        {
            EventLog.WriteEntry(ServiceName + " stopped");
            PageLoadTimer.Enabled = false;
        }

        protected void WritePageToDb(object source, ElapsedEventArgs e)
        {
            try
            {
                string html;
                using(WebClient client = new WebClient())
                {
                    html = client.DownloadString("http://stackoverflow.com");
                }
                EventLog.WriteEntry("html = " + html.Substring(0, 100));
                using(SqlConnection connection = new SqlConnection("Data Source=DESKTOP-300NQR3\\SQLEXPRESS;Initial Catalog=SoArchive;Integrated Security=True"))
                {
                    string nonQuery = $"INSERT INTO [dbo].[Homepage] ([Html]) VALUES ('{html}')";
                    using(SqlCommand command = new SqlCommand(nonQuery, connection))
                    {
                        bool succeeded = command.ExecuteNonQuery() == 1;
                        EventLog.WriteEntry(succeeded ? "Saved!" : "Not Saved");
                    }
                }
            }
            catch(Exception ex)
            {
                EventLog.WriteEntry("uh-oh! " + ex.Message);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new LrcArchiver());
        }
    }
}

我已经安装并启动了它,但是 WritePageToDb 方法似乎没有被触发,因为它的 WriteEntrys 都没有显示。我看到了

SO 归档器已启动

但在那之后什么都没有。

知道为什么会这样,或者我可以如何调试以找到原因吗?

【问题讨论】:

    标签: c# .net windows service windows-services


    【解决方案1】:

    您只是将其设置为工作,但您没有启动计时器。

    protected override void OnStart(string[] args)
    {
        PageLoadTimer.Enabled = true;
        EventLog.WriteEntry(ServiceName + " started");
    }
    

    【讨论】:

      【解决方案2】:

      试试这个,我在某个时间点遇到了问题,这对我有用。本部分示例中定时器设置为 1 分钟

        TimerCallback callbacktimer;
        Timer IntervalCheck;
      
      
        protected override void OnStart(string[] args) {
      
         try {
      
          callbacktimer = new TimerCallback(SomeMethode);
          IntervalCheck = new Timer(callbacktimer, null, TimeSpan.Zero, new TimeSpan(0, 1, 0));
      
      
      
          base.OnStart(args);
         } catch (Exception ex) {
      
         }
      
        }
      
        private void SomeMethode(object o) {
      
      
        }
      

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 2020-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 2014-04-09
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多