473,698 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 .DataManagerFac ade 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.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Diagnost ics;

using System.ServiceP rocess;

namespace StockManagerSer vice

{

public class StockMgrService : System.ServiceP rocess.ServiceB ase

{

private System.Componen tModel.IContain er components;

private System.Windows. Forms.Timer timer1;

private DatabaseManager .DataManagerFac ade dmf;

public StockMgrService ()

{

// This call is required by the Windows.Forms Component
Designer.

InitializeCompo nent();

}

// The main entry point for the process

static void Main()

{

System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[] {new
Service1(), new MySecondUserSer vice()};

//

ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
StockMgrService () };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()

{

this.components = new System.Componen tModel.Containe r();

this.timer1 = new System.Windows. Forms.Timer(thi s.components);

Trace.Listeners .Add(new EventLogTraceLi stener("StockMg rService"));

//

// timer1

//

this.timer1.Ena bled = true;

this.timer1.Int erval = 3000;

this.timer1.Tic k += new System.EventHan dler(this.timer 1_Tick);

//

// StockMgrService

//

this.ServiceNam e = "StockMgrServic e";

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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.ToStrin g(DateTime.Now) );

dmf=DatabaseMan ager.DataManage rFacade.GetInst ance();

Trace.WriteLine ("Status: Started" +
Convert.ToStrin g(DateTime.Now) );

this.timer1.Sta rt();

}
/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

Trace.WriteLine ("Status: Service Stopped" +
Convert.ToStrin g(DateTime.Now) );

}

private void timer1_Tick(obj ect sender, System.EventArg s e)

{

Trace.WriteLine ("Tick: Test" + Convert.ToStrin g(DateTime.Now) );

}

}

}
Nov 17 '05 #1
3 2193
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.co m

"Nathan Kovac" <na**@tctelco.n et> wrote in message
news:%2******** ********@TK2MSF TNGP12.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 .DataManagerFac ade 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.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Diagnost ics;

using System.ServiceP rocess;

namespace StockManagerSer vice

{

public class StockMgrService : System.ServiceP rocess.ServiceB ase

{

private System.Componen tModel.IContain er components;

private System.Windows. Forms.Timer timer1;

private DatabaseManager .DataManagerFac ade dmf;

public StockMgrService ()

{

// This call is required by the Windows.Forms Component
Designer.

InitializeCompo nent();

}

// The main entry point for the process

static void Main()

{

System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[] {new
Service1(), new MySecondUserSer vice()};

//

ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
StockMgrService () };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()

{

this.components = new System.Componen tModel.Containe r();

this.timer1 = new System.Windows. Forms.Timer(thi s.components);

Trace.Listeners .Add(new EventLogTraceLi stener("StockMg rService"));

//

// timer1

//

this.timer1.Ena bled = true;

this.timer1.Int erval = 3000;

this.timer1.Tic k += new System.EventHan dler(this.timer 1_Tick);

//

// StockMgrService

//

this.ServiceNam e = "StockMgrServic e";

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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.ToStrin g(DateTime.Now) );

dmf=DatabaseMan ager.DataManage rFacade.GetInst ance();

Trace.WriteLine ("Status: Started" +
Convert.ToStrin g(DateTime.Now) );

this.timer1.Sta rt();

}
/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

Trace.WriteLine ("Status: Service Stopped" +
Convert.ToStrin g(DateTime.Now) );

}

private void timer1_Tick(obj ect sender, System.EventArg s e)

{

Trace.WriteLine ("Tick: Test" + Convert.ToStrin g(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.c om> wrote in
message news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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.co m

"Nathan Kovac" <na**@tctelco.n et> wrote in message
news:%2******** ********@TK2MSF TNGP12.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 .DataManagerFac ade 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.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Diagnost ics;

using System.ServiceP rocess;

namespace StockManagerSer vice

{

public class StockMgrService : System.ServiceP rocess.ServiceB ase

{

private System.Componen tModel.IContain er components;

private System.Windows. Forms.Timer timer1;

private DatabaseManager .DataManagerFac ade dmf;

public StockMgrService ()

{

// This call is required by the Windows.Forms Component
Designer.

InitializeCompo nent();

}

// The main entry point for the process

static void Main()

{

System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[]
{new Service1(), new MySecondUserSer vice()};

//

ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
StockMgrService () };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()

{

this.components = new System.Componen tModel.Containe r();

this.timer1 = new System.Windows. Forms.Timer(thi s.components);

Trace.Listeners .Add(new EventLogTraceLi stener("StockMg rService"));

//

// timer1

//

this.timer1.Ena bled = true;

this.timer1.Int erval = 3000;

this.timer1.Tic k += new System.EventHan dler(this.timer 1_Tick);

//

// StockMgrService

//

this.ServiceNam e = "StockMgrServic e";

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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.ToStrin g(DateTime.Now) );

dmf=DatabaseMan ager.DataManage rFacade.GetInst ance();

Trace.WriteLine ("Status: Started" +
Convert.ToStrin g(DateTime.Now) );

this.timer1.Sta rt();

}
/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

Trace.WriteLine ("Status: Service Stopped" +
Convert.ToStrin g(DateTime.Now) );

}

private void timer1_Tick(obj ect sender, System.EventArg s e)

{

Trace.WriteLine ("Tick: Test" +
Convert.ToStrin g(DateTime.Now) );

}

}

}


Nov 17 '05 #3
Thanks for the affirmation. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Nathan Kovac" <na**@tctelco.n et> wrote in message
news:es******** ********@TK2MSF TNGP12.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.c om> wrote
in message news:%2******** **********@TK2M SFTNGP09.phx.gb l...
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.co m

"Nathan Kovac" <na**@tctelco.n et> wrote in message
news:%2******** ********@TK2MSF TNGP12.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 .DataManagerFac ade 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.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Diagnost ics;

using System.ServiceP rocess;

namespace StockManagerSer vice

{

public class StockMgrService : System.ServiceP rocess.ServiceB ase

{

private System.Componen tModel.IContain er components;

private System.Windows. Forms.Timer timer1;

private DatabaseManager .DataManagerFac ade dmf;

public StockMgrService ()

{

// This call is required by the Windows.Forms Component
Designer.

InitializeCompo nent();

}

// The main entry point for the process

static void Main()

{

System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[]
{new Service1(), new MySecondUserSer vice()};

//

ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
StockMgrService () };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()

{

this.components = new System.Componen tModel.Containe r();

this.timer1 = new System.Windows. Forms.Timer(thi s.components);

Trace.Listeners .Add(new
EventLogTraceLi stener("StockMg rService"));

//

// timer1

//

this.timer1.Ena bled = true;

this.timer1.Int erval = 3000;

this.timer1.Tic k += new System.EventHan dler(this.timer 1_Tick);

//

// StockMgrService

//

this.ServiceNam e = "StockMgrServic e";

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Disp ose();

}

}

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.ToStrin g(DateTime.Now) );

dmf=DatabaseMan ager.DataManage rFacade.GetInst ance();

Trace.WriteLine ("Status: Started" +
Convert.ToStrin g(DateTime.Now) );

this.timer1.Sta rt();

}
/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

Trace.WriteLine ("Status: Service Stopped" +
Convert.ToStrin g(DateTime.Now) );

}

private void timer1_Tick(obj ect sender, System.EventArg s e)

{

Trace.WriteLine ("Tick: Test" +
Convert.ToStrin g(DateTime.Now) );

}

}

}



Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1684
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 (i have never threaded before, so excuse my ignorance) 2. If i have instances of the catalogue class in both those timers, will there be an error? for example, if Timer1 loops every 10 mins, and Timer2 loops every 3
3
2033
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 page in the site, that page (.aspx) instantiates a compiled class (.DLL). The instantiation process creates a Timer that runs in the background to perform tasks every so often (such as notifying clients
1
1121
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 event handler, and each invocation sends a command to another service. What are the threading considerations given this scenario?
2
1341
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 event handler, and each invocation sends a command to another service. What are the threading considerations given this scenario?
1
1901
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 minutes, remove it form the collection. Thank you, Daniel
2
2210
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 Windows Service. That is, the Timer won't fire again once it's stopped (Timer.Stop() or Timer.Enabled = False) (http://support.microsoft.com/kb/842793/en-us). Instead of using Threading.Timer, can I just have the code to run inside the...
1
1827
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 box.. I have created a service to grab data off a receive socket (small packets), place in a queue (Queue class), and do an insert into SQL. I have 3 timers in my project. One checks the status of the sql service every 15 seconds, one checks the...
5
12227
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
8
3368
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 on a network for files and then copies these files to another location (on the network) AND then updates a record in the database. The file copying is performed before the database update because the file system is not transactional. The code...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.