【问题标题】:C# Windows Service - Timer is not working in debuggingC# Windows 服务 - 定时器在调试中不起作用
【发布时间】:2017-03-08 16:15:59
【问题描述】:

我已经开始用 c# 语言构建 windows 服务。我想在里面实现定时器功能。但是由于某种原因,DoIt 计时器事件处理程序在调试期间没有被触发,我也没有得到任何异常。我正在尝试使用 Debug->Start new instance 调试 Windows 服务。

行 TraceLog.WriteTrace("Router Service Started");确实会被击中并处决。

   public partial class EntryPoint : ServiceBase
    {
        private const int TIMER_INTERVAL = 10000;
        private System.Timers.Timer mvTimer = new System.Timers.Timer();

        [MTAThread()]
        [System.Diagnostics.DebuggerNonUserCode()]
        public static void Main()
        {
            #if DEBUG
                        EntryPoint service = new EntryPoint();
                        service.Start();
            #else
                        ServiceBase[] ServicesToRun;
                        ServicesToRun = new ServiceBase[] 
                        { 
                            new EntryPoint() 
                        };
                        ServiceBase.Run(ServicesToRun);
            #endif
        }
        public EntryPoint()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            InitializeComponent();
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            if (ex != null)
            {
                Console.WriteLine(ex);
            }
        }


        public void Start()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {
            TraceLog.SetTrace(ConfigurationManager.AppSettings["RouterTraceLog"]);
            TraceLog.WriteTrace("Router Service Started");

            mvTimer = new Timer();
            mvTimer.Elapsed += new ElapsedEventHandler(DoIt);
            try
            {
                mvTimer.Interval = TIMER_INTERVAL;
                mvTimer.Enabled = true;                
            }
            catch (Exception ex)
            {
                mvTimer.Interval = TIMER_INTERVAL;
                TraceLog.WriteTrace(ex.Message);
            }
        }

        private void DoIt(object sender, ElapsedEventArgs e)
        {
            TraceLog.WriteTrace("Inside DoIt :: " + DateTime.UtcNow.ToString());
        }

        protected override void OnStop()
        {
            mvTimer.Enabled = false;
            TraceLog.WriteTrace("Router Service stopping");
        }

请提出建议。我错过了一些非常小的东西,无法确定。

【问题讨论】:

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


    【解决方案1】:

    在调试模式下,应用程序在函数OnStart 完成后退出。没有什么可以阻止应用程序退出。

    您需要添加Console.ReadLine(); 以防止应用程序退出。

    #if DEBUG
          EntryPoint service = new EntryPoint();
          service.Start();
          Console.ReadLine();
    #else
    

    您可以在MSDN 阅读有关如何:调试 Windows 服务应用程序的更多信息

    【讨论】:

    • 我试过了,但发现使用 ReadLine() 没有区别。它还在做出口。
    • 输出类型是否设置为控制台应用程序?它没有理由退出
    • 是的,在我将输出类型更改为控制台应用程序之后,它起作用了 :-)。非常感谢 :-)
    猜你喜欢
    • 2014-10-13
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2017-09-30
    • 2021-12-18
    相关资源
    最近更新 更多