views:

96

answers:

4

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.

A: 

You aren't showing the code that creates the timer object. What have you set the timer interval to?

ScottTx
i followed the tutorial posted. I dragged and dropped a timer in the design view from the toolbox, under the components tab. interval is 60000
Joseph Stein
+1  A: 

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
+1  A: 

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) : http://weblogs.asp.net/sibrahim/archive/2004/01/13/58429.aspx

la mouette
you were right as I suspected. I changed up the code and it works now. thanks!
Joseph Stein
A: 

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?

JustBoo
nope. just the two I mentioned in my post
Joseph Stein