如何让我的Windows服务继续运行?

时间:2012-05-08 14:17:40

标签: c# windows-services

我正在尝试让Windows服务一直在我的计算机后台运行而且没有人知道。我的Windows服务下载了我的电子邮件收件箱的内容并将其放入我的数据库中。

我的Windows服务只是接缝停止 - 它每隔60秒输入一次日志然后停止大约10分钟?

我在下面发布了我的代码。任何人都可以看到或告诉我一个原因吗?

非常感谢任何帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace EmailWindowsService
{
    public partial class MyEmailService : ServiceBase
    {
        private DateTime lastRun;
        private bool flag = true;
        private static System.Timers.Timer aTimer;
        public MyEmailService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource")) // every thing the windows service does is logged in server explorer
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLogEmail.Source = "MySource";
            eventLogEmail.Log = "MyNewLog";

            // Timer Code
             aTimer = new System.Timers.Timer(1 * 60 * 1000); // 60 seconds
             aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
             aTimer.Enabled = true;
            // Timer Code
        }

        protected override void OnStart(string[] args)
        {
            flag = true;
            lastRun = DateTime.Now;
            eventLogEmail.WriteEntry("Started");
        }

        protected override void OnStop()
        {
            eventLogEmail.WriteEntry("Stopped");
        }
        protected override void OnPause()
        {
            eventLogEmail.WriteEntry("Paused");
        }
        protected override void OnContinue()
        {    
            eventLogEmail.WriteEntry("Continuing");
        }
        protected override void OnShutdown()
        {
            eventLogEmail.WriteEntry("ShutDowned");
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            RetriveEmailClass Emails = new RetriveEmailClass();
            if (flag == true)
            {
                eventLogEmail.WriteEntry("In getting Email Method");
                Emails.ServiceEmailMethod();
                lastRun = DateTime.Now;
                flag = false;
            }
            else if (flag == false)
            {
                if (lastRun.Date < DateTime.Now.Date)
                {
                    Emails.ServiceEmailMethod();
                    eventLogEmail.WriteEntry("In getting Email Method");
                }
            }
        }       
        }
    }

4 个答案:

答案 0 :(得分:0)

除了Emails.ServiceEmailMethod方法的源代码外,您的代码非常简单。该方法是否会生成任何异常?如果是这样,他们没有被困在你的计时器方法中。尝试:

try { Emails.ServiceEmailMethod(); }  
catch (Exception ex) { eventLogEmail.WriteEntry(ex.Message); }

答案 1 :(得分:0)

Windows服务停止运行的几个原因。

1. Unhandled exception in your code. Looking from you code snippet, please add exception handling in the OnTimedEvent() method.
2. You service may crashed for some reason. In this case, you can go to event viewer to find out the reason for the failure. 

希望这有帮助。

答案 2 :(得分:0)

看到你的班级没有错误,错误可能会让你失去整个服务。 另外,请尝试将计时器放入方法中,只调用它,而不是在服务代码中使用它。

应始终将Windows服务设置为只调用方法的空shell。

答案 3 :(得分:0)

你很可能有一个未处理的例外。它是隐藏的,因为您使用System.Timers.Timer。该计时器会吃掉所有未处理的异常,而不是让它们崩溃你的应用程序。

这意味着您的应用可能看起来运行正常,而不是。计时器回调中的try / catch将证明这一点。

我建议您使用System.Threading.Timer,因为它不会以这种方式工作。

相关问题