简体   繁体   中英

Stuck on implementing Windows Service

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.

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}

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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM