Timers and Windows Service question

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));

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Nathan,

The reason for this is that the timer you are using is in the
System.Windows.Forms namespace, where the notifications depend on a window
being shown and messages processed. However, in a service, you don't have a
user session to create a window in, and you can't receive messages.

The solution to this is to use the Timer class in the System.Timers
namespace, which will allow you to create timers that are not dependent on
windows.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
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));

}

}

}
 
N

Nathan Kovac

Thanks, I figured it was overlooking something simple. You know you deserve
that MVP.


Nicholas Paldino said:
Nathan,

The reason for this is that the timer you are using is in the
System.Windows.Forms namespace, where the notifications depend on a window
being shown and messages processed. However, in a service, you don't have
a user session to create a window in, and you can't receive messages.

The solution to this is to use the Timer class in the System.Timers
namespace, which will allow you to create timers that are not dependent on
windows.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
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));

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Thanks for the affirmation. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Nathan Kovac said:
Thanks, I figured it was overlooking something simple. You know you
deserve that MVP.


Nicholas Paldino said:
Nathan,

The reason for this is that the timer you are using is in the
System.Windows.Forms namespace, where the notifications depend on a
window being shown and messages processed. However, in a service, you
don't have a user session to create a window in, and you can't receive
messages.

The solution to this is to use the Timer class in the System.Timers
namespace, which will allow you to create timers that are not dependent
on windows.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Kovac said:
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));

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top