Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get my Windows Service to keep running?
    primarykey
    data
    text
    <p>I am trying to have a Windows service run all the time in the background of my computer with no-one knowing. My Windows service downloads the content of my email inbox and puts it in my database.</p> <p>My Windows service just seams to stop - it enters a log every 60 seconds and then stops about 10 mins in?</p> <p>I have posted my code below. Can any one see or tell me a reason why?</p> <p>Any help would be much appreciated.</p> <pre><code>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 &lt; DateTime.Now.Date) { Emails.ServiceEmailMethod(); eventLogEmail.WriteEntry("In getting Email Method"); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload