0

hey guys, I've followed some tutorials online regarding creating and installing a windows service and seem to keep getting into a bind. I followed the tutorial here and it while it seems to be working, its not 100%. This is the code im using:

namespace SuperService
{
partial class Logger : ServiceBase
{
    public Logger()
    {
        InitializeComponent();
    }

    void timer1_Tick( object sender, EventArgs e )
    {
        LogEvent( "This Timer has been ticked!", EventLogEntryType.Information );
    }

    protected override void OnStart( string[] args )
    {
        timer1.Tick += new EventHandler( timer1_Tick );
        timer1.Start();
        LogEvent( "This SuperService has started!", EventLogEntryType.Information );
    }

    protected override void OnStop()
    {
        LogEvent( "This SuperService has stopped.", EventLogEntryType.Information );
    }

    protected override void OnPause()
    {
        base.OnPause();
        timer1.Stop();
    }

    protected override void OnContinue()
    {
        base.OnContinue();
        timer1.Start();
    }

    static void LogEvent( String Message, EventLogEntryType type )
    {
        String source = "Logger";
        String log = "Application";
        if (!EventLog.SourceExists( source ))
        {
            EventLog.CreateEventSource( source, log );
        }

        EventLog eLog = new EventLog();
        eLog.Source = source;

        eLog.WriteEntry( Message, type );
    }
}
}

Now when I check the Event Viewer after starting the service it shows the following two events:

This SuperService has started!

Service started successfully.

So it seems to be working somewhat, what I dont see is the event triggered by timer1_Tick. Does anyone know why or can point me in the right direction pls? Thanks in advance.

Community
  • 1
  • 1
Joey
  • 525
  • 1
  • 6
  • 16
  • What is the interval of the timer? Make sure it is not 0. – Albin Sunnanbo Aug 19 '10 at 20:43
  • Where is the timer's interval set? Maybe in the designer? And what value does it have? – Marc Gravell Aug 19 '10 at 20:45
  • 60000 = 60 seconds. are you waiting that long? set it to something lower, like 1000 (1 second). – Derick Bailey Aug 19 '10 at 20:49
  • 1
    Which type of timer is this?? There are at least 3 different `Timer` classes in .NET - I had the best experiences with `System.Timers.Timer` - both others had issues... – marc_s Aug 19 '10 at 20:49
  • not sure really. i followed the tutorial posted. I dragged and dropped a timer in the design view from the toolbox, under the components tab. – Joey Aug 19 '10 at 20:52

3 Answers3

1

As an aside, windows services are much easier with topshelf. Open source project that allows you to write your service as a console app/POCOs, but pick up install/uninstall/debug support from a "service container" that abstracts away all the glue.

myservice                                  (to run as console app for debugging)
myservice /install
myservice /uninstall
myservice /instance:{instance_name}
Rob
  • 5,525
  • 1
  • 24
  • 26
1

Do you use System.Threading.Timer instead of the System.Windows.Forms.Timer ? The link of the tutorial you use have comments of user having the same issue and switching to System.Threading.Timer with success

for more information (taken from your link) : Problem with System.Timers.Timer not firing in a Windows service.

aledpardo
  • 761
  • 9
  • 19
la mouette
  • 71
  • 1
0

Edit: Here is a SO answer covering the very thing I write about below.

Windows Service Stops Automatically


I believe Windows will stop a service if it "thinks" the service has nothing to do. I can't remember the exact underlying "API" that does this. I fought with it a few years back.


Is there something in your event log with the wording of: "Nothing for myservice to do. Terminating." Or some such message that conveys that verbiage?

Community
  • 1
  • 1
JustBoo
  • 1,723
  • 9
  • 9