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

Timers and Windows Service question

P: n/a
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(ServicesToRu n);

}

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

}

}

}
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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(ServicesToRu n);

}

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

}

}

}

Nov 17 '05 #2

P: n/a
Thanks, I figured it was overlooking something simple. You know you deserve
that MVP.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2******************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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(ServicesToRu n);

}

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

}

}

}


Nov 17 '05 #3

P: n/a
Thanks for the affirmation. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Nathan Kovac" <na**@tctelco.net> wrote in message
news:es****************@TK2MSFTNGP12.phx.gbl...
Thanks, I figured it was overlooking something simple. You know you
deserve that MVP.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2******************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Nathan Kovac" <na**@tctelco.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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(ServicesToRu n);

}

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

}

}

}



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.