473,416 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Timers and Windows Service question

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
3 2173
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jason | last post by:
Hi all just a quick question. I have a windows service with a "Catalogue" class. I would like to know: if i put two "ServiceTimer" objects in my service 1. Do they run in separate threads...
3
by: Jeff Greenland | last post by:
Hello everyone, I am having problems with Timers in a web application. They just seem to stop running after 15 minutes or so. My web application is set up like this: When a user hits a...
1
by: jeff | last post by:
Greetings; Newbie here, please forgive my ignorance of the vb.net threading model. I am developing a windows service which is driven by a variable number of timers. All timers use the same...
2
by: jeff | last post by:
Greetings; Newbie here, please forgive my ignorance of the vb.net threading model. I am developing a windows service which is driven by a variable number of timers. All timers invoke the same...
1
by: Bamse | last post by:
Hi, can timers be used in webservices? as an example: to check at some time interval an object - Application and for each logged user, check its login period, and if it is greater than 30...
2
by: cntams | last post by:
All, I have a Windows Service and it has one System.Timers.Timer that fires every 500 milliseconds. Now I have noticed that there's a bug in System.Timers.Timer when it's being used combined with...
1
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.