By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

Time in Service

P: n/a
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.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Reflection;
using System.Configuration;

using System.Xml;
using System.Xml.XPath;
using Dell.WWOps.GlobalShip.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.Timer srvcTimer;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

// 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.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
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 InitializeComponent()
{
this.srvcTimer = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.s rvcTimer)).BeginInit();
//
// srvcTimer
//
this.srvcTimer.Interval = 5000;
this.srvcTimer.Elapsed += new
System.Timers.ElapsedEventHandler(this.srvcTimer_E lapsed);
//
// DELL_QWatcher
//
this.ServiceName = "DELL_QWatcher";
((System.ComponentModel.ISupportInitialize)(this.s rvcTimer)).EndInit();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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.ToDouble(ConfigurationSettings.AppSettings["interval"].ToString());
this.srvcTimer.Enabled = true;
}

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

private void srvcTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs 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
GlobalShipSystem oGS = GlobalShipSystem.Instance;
XPathNavigator nav = oGS.GetNextOrder();

if (nav != null)
{

}

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

}
}
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.