N
Nathan Kovac
I have a feeling I am missing something simple, but I just can't find it.
Perhaps someone can give me a lead on where to look. I will describe the
issue then post my code to the web service. My issue is simply getting
timers to work. I have a DatabaseManager.DataManagerFacade which contains a
timer. Every 6 seconds it updates stock data using an online webservice. I
wrote have 2 possible startup projects to make the service work. One is a
windows form the other is a windows service. Everything works fine in the
windows form.
In the windows service I do the same thing I do in the windows form; create
an instance. But the timers were not being triggered.
I approached debugging this by adding some entries to the windows event log
as the service starts up. All the startup and stop events worked correctly.
I then tried adding a timer, which can be seen in the code below, to write
to the application log every 3 seconds. This timer is also not being fired.
If you have any ideas or have gotten timers to work fine in windows services
please post a reply here.
Thanks,
Nathan
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
namespace StockManagerService
{
public class StockMgrService : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private DatabaseManager.DataManagerFacade dmf;
public StockMgrService()
{
// This call is required by the Windows.Forms Component
Designer.
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.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 System.ServiceProcess.ServiceBase[] { new
StockMgrService() };
System.ServiceProcess.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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
Trace.Listeners.Add(new EventLogTraceListener("StockMgrService"));
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 3000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// StockMgrService
//
this.ServiceName = "StockMgrService";
}
/// <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)
{
Trace.WriteLine("Status: Starting" +
Convert.ToString(DateTime.Now));
dmf=DatabaseManager.DataManagerFacade.GetInstance();
Trace.WriteLine("Status: Started" +
Convert.ToString(DateTime.Now));
this.timer1.Start();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
Trace.WriteLine("Status: Service Stopped" +
Convert.ToString(DateTime.Now));
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Trace.WriteLine("Tick: Test" + Convert.ToString(DateTime.Now));
}
}
}
Perhaps someone can give me a lead on where to look. I will describe the
issue then post my code to the web service. My issue is simply getting
timers to work. I have a DatabaseManager.DataManagerFacade which contains a
timer. Every 6 seconds it updates stock data using an online webservice. I
wrote have 2 possible startup projects to make the service work. One is a
windows form the other is a windows service. Everything works fine in the
windows form.
In the windows service I do the same thing I do in the windows form; create
an instance. But the timers were not being triggered.
I approached debugging this by adding some entries to the windows event log
as the service starts up. All the startup and stop events worked correctly.
I then tried adding a timer, which can be seen in the code below, to write
to the application log every 3 seconds. This timer is also not being fired.
If you have any ideas or have gotten timers to work fine in windows services
please post a reply here.
Thanks,
Nathan
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
namespace StockManagerService
{
public class StockMgrService : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private DatabaseManager.DataManagerFacade dmf;
public StockMgrService()
{
// This call is required by the Windows.Forms Component
Designer.
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.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 System.ServiceProcess.ServiceBase[] { new
StockMgrService() };
System.ServiceProcess.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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
Trace.Listeners.Add(new EventLogTraceListener("StockMgrService"));
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 3000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// StockMgrService
//
this.ServiceName = "StockMgrService";
}
/// <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)
{
Trace.WriteLine("Status: Starting" +
Convert.ToString(DateTime.Now));
dmf=DatabaseManager.DataManagerFacade.GetInstance();
Trace.WriteLine("Status: Started" +
Convert.ToString(DateTime.Now));
this.timer1.Start();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
Trace.WriteLine("Status: Service Stopped" +
Convert.ToString(DateTime.Now));
}
private void timer1_Tick(object sender, System.EventArgs e)
{
Trace.WriteLine("Tick: Test" + Convert.ToString(DateTime.Now));
}
}
}