Time in Service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Microsoft

    Time in Service

    I'm creating a service and am using a timer in it. When the service starts
    I enable the timer, and then in the timer elasped event, I'm disabling it
    (this.srvcTimer .Enabled = false) so the timer event doesn't fire while I'm
    already in it. At the end of the timer elapsed event I re-enable it.
    However, while running in the debugger, the event never fires. If I comment
    out the line disabling the timer in the elapsed event, the event fires. I'm
    not quite sure what I'm doing wrong if anything.

    Here's the code in question, and the appropriate lines in the elapsed event
    are commented... the event in the last bit of code at the bottom...

    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Diagnost ics;
    using System.ServiceP rocess;
    using System.Reflecti on;
    using System.Configur ation;

    using System.Xml;
    using System.Xml.XPat h;
    using Dell.WWOps.Glob alShip.External ;

    namespace DELL_QWatcher
    {
    // TODO: learn how to create class and/or structure for holding config data
    from INI

    public class DELL_QWatcher : ServiceBase
    {
    private System.Timers.T imer srvcTimer;

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.Componen tModel.Containe r components = null;

    public DELL_QWatcher()
    {
    // This call is required by the Windows.Forms Component Designer.
    InitializeCompo nent();

    // TODO: Add any initialization after the InitComponent call
    }

    // The main entry point for the process
    static void Main()
    {
    ServiceBase[] ServicesToRun;

    // More than one user Service may run within the same process. To add
    // another service to this process, change the following line to
    // create a second service object. For example,
    //
    // ServicesToRun = new System.ServiceP rocess.ServiceB ase[] {new
    Service1(), new MySecondUserSer vice()};
    //
    ServicesToRun = new ServiceBase[] { new DELL_QWatcher() };

    ServiceBase.Run (ServicesToRun) ;
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.srvcTimer = new System.Timers.T imer();
    ((System.Compon entModel.ISuppo rtInitialize)(t his.srvcTimer)) .BeginInit();
    //
    // srvcTimer
    //
    this.srvcTimer. Interval = 5000;
    this.srvcTimer. Elapsed += new
    System.Timers.E lapsedEventHand ler(this.srvcTi mer_Elapsed);
    //
    // DELL_QWatcher
    //
    this.ServiceNam e = "DELL_QWatcher" ;
    ((System.Compon entModel.ISuppo rtInitialize)(t his.srvcTimer)) .EndInit();

    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Disp ose();
    }
    }
    base.Dispose( disposing );
    }

    /// <summary>
    /// Set things in motion so your service can do its work.
    /// </summary>
    protected override void OnStart(string[] args)
    {
    // INI processes service configuration stuff...
    // this.srvcTimer. Interval =
    Convert.ToDoubl e(Configuration Settings.AppSet tings["interval"].ToString());
    this.srvcTimer. Enabled = true;
    }

    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
    {
    this.srvcTimer. Enabled = false;
    }

    private void srvcTimer_Elaps ed(object sender,
    System.Timers.E lapsedEventArgs e)
    {
    // stop the timer, so we're no re-entrant
    // if this is commented out, the event fires as expected. However, if
    it's not commented out, this event never fires...
    // this.srvcTimer. Enabled = false;

    // TODO: add processing code here...
    // breakpoint goes here
    GlobalShipSyste m oGS = GlobalShipSyste m.Instance;
    XPathNavigator nav = oGS.GetNextOrde r();

    if (nav != null)
    {

    }

    // re-start the time
    // this.srvcTimer. Enabled = true;
    }

    }
    }


Working...