2012-05-08 33 views
1

我试图让Windows服务在我的电脑后台运行,无人知晓。我的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"); 
       } 
      } 
     }  
     } 
    } 
+1

Windows服务意外停止的原因是:1)未处理的异常2)有人关闭它。现在,添加异常处理到您的代码并查看错误的位置。 – Shai

+0

在代码中放置一个try-catch来调试它停止的原因。 –

+1

如果我错了,我很抱歉,但是你有没有问过类似的问题?你是[Pomster](http://stackoverflow.com/users/1356321/pomster)?那么为什么要创建一个新帐户?如果我们能够在其他问题的背景下看到您的问题,则更容易提供帮助。如果我错了,我很抱歉。 – Skalli

回答

0

看到你的班级没有错误,错误可能会导致你的整个服务。 也尝试把你的计时器放入一个方法,只调用它,没有它在你的服务代码。

窗口服务应该总是作为一个空壳来调用方法。

+1

http://stackoverflow.com/users/1356321/pomster – Pomster

0

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

try { Emails.ServiceEmailMethod(); } 
catch (Exception ex) { eventLogEmail.WriteEntry(ex.Message); } 
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. 

希望这有助于。

0

您很可能有未处理的异常。由于您使用了System.Timers.Timer,因此它是隐藏的。该计时器消耗所有未处理的异常,而不是让它们崩溃你的应用程序。

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

我确实建议您使用System.Threading.Timer,因为它不起作用。

相关问题