473,386 Members | 1,754 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,386 software developers and data experts.

configurable timer interval for a windows service.

Is it possible to have the timer interval in a window's service be derived
from an app.config setting?

When I implemented one a while back I was forced to hard code the value
because it wasn't working. There didn't appear to be any problem with the
code that extracts the value from the key in the config file.

Thanks,
-hazz

Jun 28 '06 #1
10 6391
Sure you can do that; just have to be sure to cast the value from the
config file to an int or double (depending on which timer you're
using).

HTH
Andy

hazz wrote:
Is it possible to have the timer interval in a window's service be derived
from an app.config setting?

When I implemented one a while back I was forced to hard code the value
because it wasn't working. There didn't appear to be any problem with the
code that extracts the value from the key in the config file.

Thanks,
-hazz


Jun 28 '06 #2
hazz,

I would argue that you shouldn't use the config file. Rather, I would
open an endpoint through remoting which has a method which will allow you to
set the timer interval. Then, you can have a UI endpoint which will set
this.

No need to stop the service and restart so you can configure the timer
interval.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"hazz" <gr*********@comcast.net> wrote in message
news:e$**************@TK2MSFTNGP03.phx.gbl...
Is it possible to have the timer interval in a window's service be derived
from an app.config setting?

When I implemented one a while back I was forced to hard code the value
because it wasn't working. There didn't appear to be any problem with the
code that extracts the value from the key in the config file.

Thanks,
-hazz

Jun 28 '06 #3
Thanks Nicholas for this excellent idea. How do I do this? Thanks. -Greg

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e9**************@TK2MSFTNGP05.phx.gbl...
hazz,

I would argue that you shouldn't use the config file. Rather, I would
open an endpoint through remoting which has a method which will allow you
to set the timer interval. Then, you can have a UI endpoint which will
set this.

No need to stop the service and restart so you can configure the timer
interval.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"hazz" <gr*********@comcast.net> wrote in message
news:e$**************@TK2MSFTNGP03.phx.gbl...
Is it possible to have the timer interval in a window's service be
derived from an app.config setting?

When I implemented one a while back I was forced to hard code the value
because it wasn't working. There didn't appear to be any problem with the
code that extracts the value from the key in the config file.

Thanks,
-hazz


Jun 29 '06 #4
Thanks for the heads up on the cast Andy. -Greg

"Andy" <aj*****@alum.rit.edu> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Sure you can do that; just have to be sure to cast the value from the
config file to an int or double (depending on which timer you're
using).

HTH
Andy

hazz wrote:
Is it possible to have the timer interval in a window's service be
derived
from an app.config setting?

When I implemented one a while back I was forced to hard code the value
because it wasn't working. There didn't appear to be any problem with the
code that extracts the value from the key in the config file.

Thanks,
-hazz

Jun 29 '06 #5
You'd probably need to look into either remoting or using System.Net
and doing some socket level stuff.

It is much more involved than reading a setting from a config file;
whether or not you implement this idea depends on how important it is
to be able to change the timer intervals without restarting the
service.

Keep in mind too that if you have a network port open for this purpose,
anyone else might be able to send data to your service as well.

HTH
Andy

hazz wrote:
Thanks Nicholas for this excellent idea. How do I do this? Thanks. -Greg


Jun 29 '06 #6
Maybe the following would be good enough. If during the init process of the
service, when its started or restarted, it can obtain a new interval from a
config file. The exe won't have to recompiled/redeploy if someone wants to
change the interval. Thanks, -Greg

private void InitializeComponent()

System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
int interval =
Convert.ToInt32(System.Configuration.Configuration Settings.AppSettings["interval"]);
timer1.Interval = interval ;
timer1.Enabled = true;

"Andy" <aj*****@alum.rit.edu> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
You'd probably need to look into either remoting or using System.Net
and doing some socket level stuff.

It is much more involved than reading a setting from a config file;
whether or not you implement this idea depends on how important it is
to be able to change the timer intervals without restarting the
service.

Keep in mind too that if you have a network port open for this purpose,
anyone else might be able to send data to your service as well.

HTH
Andy

hazz wrote:
Thanks Nicholas for this excellent idea. How do I do this?
Thanks. -Greg

Jun 29 '06 #7
I changed that to double as that is what the interval property is.

double interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
timer1.Interval = interval;

But it doesn't work at all. The hard code value did work. hmmmmm.......

"hazz" <gr*********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Maybe the following would be good enough. If during the init process of
the service, when its started or restarted, it can obtain a new interval
from a config file. The exe won't have to recompiled/redeploy if someone
wants to change the interval. Thanks, -Greg

private void InitializeComponent()

System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
int interval =
Convert.ToInt32(System.Configuration.Configuration Settings.AppSettings["interval"]);
timer1.Interval = interval ;
timer1.Enabled = true;

"Andy" <aj*****@alum.rit.edu> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
You'd probably need to look into either remoting or using System.Net
and doing some socket level stuff.

It is much more involved than reading a setting from a config file;
whether or not you implement this idea depends on how important it is
to be able to change the timer intervals without restarting the
service.

Keep in mind too that if you have a network port open for this purpose,
anyone else might be able to send data to your service as well.

HTH
Andy

hazz wrote:
Thanks Nicholas for this excellent idea. How do I do this?
hanks. -Greg


Jun 29 '06 #8
Error: The designer cannot process the code at line 56:
interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
The code within the method 'InitializeComponent' is generated by the
designer and should not be manually modified. Please remove any changes and
try opening the designer again.

hmmmm

"hazz" <gr*********@comcast.net> wrote in message
news:eJ*************@TK2MSFTNGP03.phx.gbl...
I changed that to double as that is what the interval property is.

double interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
timer1.Interval = interval;

But it doesn't work at all. The hard code value did work. hmmmmm.......

"hazz" <gr*********@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Maybe the following would be good enough. If during the init process of
the service, when its started or restarted, it can obtain a new interval
from a config file. The exe won't have to recompiled/redeploy if someone
wants to change the interval. Thanks, -Greg

private void InitializeComponent()

System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
int interval =
Convert.ToInt32(System.Configuration.Configuration Settings.AppSettings["interval"]);
timer1.Interval = interval ;
timer1.Enabled = true;

"Andy" <aj*****@alum.rit.edu> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
You'd probably need to look into either remoting or using System.Net
and doing some socket level stuff.

It is much more involved than reading a setting from a config file;
whether or not you implement this idea depends on how important it is
to be able to change the timer intervals without restarting the
service.

Keep in mind too that if you have a network port open for this purpose,
anyone else might be able to send data to your service as well.

HTH
Andy

hazz wrote:
Thanks Nicholas for this excellent idea. How do I do this?
anks. -Greg



Jun 29 '06 #9
Well, do what it says, and move the code out of the
InitializeComponent.

I suggest you move it into the OnStart handler.

Also, doing your Convert should be wrapped in a Try Catch incase the
value doesn't exist or isn't a double number; in your catch, you can
throw the exception, or set a default interval value (and log a
warning).

HTH
Andy
hazz wrote:
Error: The designer cannot process the code at line 56:
interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
The code within the method 'InitializeComponent' is generated by the
designer and should not be manually modified. Please remove any changes and
try opening the designer again.

hmmmm


Jun 30 '06 #10
Thanks Andy. I got the configurable timer interval service to work !
I followed your advise and that of Peter in a later post. I moved all
the code to the onstart and made the timer and interval static at the class
level
I used MessageBoxes throughout the OnStart try block as needed to find out
which lines of code were working and which weren't.
That is how I discovered I needed to make the timer a static at the class
level because it was erroring out saying the object hadn't been created, ie.
the timer.
Then I deleted those but kept the messagebox in the catch block for the
moment.
Relevant code is below.
Thanks again. -Greg

public class Service1 : System.ServiceProcess.ServiceBase
{
static public System.Timers.Timer timer1;
static double interval;
***code **
protected override void OnStart(string[] args)
{
try
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(timer1_Elapsed);
interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show("In OnStart Component" + ex.ToString());
}
"Andy" <aj*****@alum.rit.edu> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Well, do what it says, and move the code out of the
InitializeComponent.

I suggest you move it into the OnStart handler.

Also, doing your Convert should be wrapped in a Try Catch incase the
value doesn't exist or isn't a double number; in your catch, you can
throw the exception, or set a default interval value (and log a
warning).

HTH
Andy
hazz wrote:
Error: The designer cannot process the code at line 56:
interval =
Convert.ToDouble(System.Configuration.Configuratio nSettings.AppSettings["interval"]);
The code within the method 'InitializeComponent' is generated by the
designer and should not be manually modified. Please remove any changes
and
try opening the designer again.

hmmmm

Jun 30 '06 #11

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

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
2
by: DaveF | last post by:
I am trying to write a service to fire an ftp object off. I want to wait 2 minutes and then download the files. Then repeat every 2 minutes. The files just seem to stop downloading. Does that timer...
7
by: J. Hill | last post by:
I have a Windows Service with a timer but the .Tick event is not being fired/called. Don't know what code to include...I enabled and started the timer...I have the exact same code in a Windows...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
1
by: Roland | last post by:
Hi, I have written a Windows Service with a Timer (System). The Interval is set to 60000 (60 seconds) for testing. In the Timer1_Elapsed event I execute a task. In this case I have to read a...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
9
by: archana | last post by:
Hi all, I want to know about interval of timer. I am using timer in windows service.I head somewhere that when i set interval property of timer while setting interval, restart time of Pc is...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
4
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...

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.