Timer not working on my Service.

J

janhm

Hello.

I have created a windows service that is supposed to turn the monitor on/off
at specific times a day, but my timer dosn't seem to work.

on my Service Start i set :

timer1.Enabled = true;
timer1.Interval = 1500;

and on my timer_tick i set this.

int intHour = DateTime.Now.Hour;

if (intHour < 7)
SendMessage((int)-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
else if (intHour > 20)
SendMessage((int)-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
else
SendMessage((int)-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);


but it dosnt work.

i tried setting

System.Threading.Thread.Sleep(3000);
SendMessage((int)-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
System.Threading.Thread.Sleep(10000);
SendMessage((int)-1, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);

on Service Start, and this works fine, so I know my Sendmessage works.



Any advice?


/Jan
 
R

RobbGMelenyk

I believe you need a System.Timers.Timer which supports Timer_Elapsed
event. You are using a System.Windows.Forms.Timer which uses
Timer_Tick event. I believe the timer you want is in the Components
section of the toolbox.
 
J

janhm

Thanks Robb.

I see your point, however I did use the one from the Components section of
the toolbox.

/Jan
 
G

Guest

janhm,
A windows service seems like an awful lot of overhead just for something
that really needs to only execute 2X a day.
Why not just convert this to a console app and set it to run at the
appropriate times in Task Scheduler?
Peter
 
R

RobbGMelenyk

Interesting. My particular componets timer does not suppoer the Tick
event. I've had this problem before with my own services, and
switching to the System.Timers.Timer fixed it.
 
J

janhm

I thought about that also.. but i need to have a this running with a Secure
Browser (www.sitekiosk.com) so instead of having to set up the browser to
allow this app to run, and it would also need to be deployed on 180+
machines. I thought just installing a service would be easier.

But the service solution might not be the best way around this.

/Jan
 
J

janhm

Strange.. I checked it twice.. Im using vs 2005

Interesting. My particular componets timer does not suppoer the Tick
event. I've had this problem before with my own services, and
switching to the System.Timers.Timer fixed it.
 
J

janhm

Ok, i removed the timer i selected from the toobox and added this:

timer.Elapsed += new ElapsedEventHandler(OnElapsed);

timer.Interval = 10000;
timer.AutoReset = true;
timer.Start();

void OnElapsed(Object sender, ElapsedEventArgs e)
{
// do stuff here
}

this seems to work, thanks Robb, for pointing me in the right direction. :)

But this brings me to Peters respons. Would this be a bad solution to use a
Service for this ?


/Jan
 
R

RobbGMelenyk

I apologize if i'm interjecting, but it seems Peter thought you were
using it on a home system, and no plans of any sort of deployment. I
think that the service would be easier to manage, in your case of
deployment. If I were doing this at home, yea I'd just write a console
app.

Again sorry if it's not my place.
 
J

janhm

I apologize if i'm interjecting, but it seems Peter thought you were
using it on a home system, and no plans of any sort of deployment. I
think that the service would be easier to manage, in your case of
deployment. If I were doing this at home, yea I'd just write a console
app.

Again sorry if it's not my place.


Any help is welcome :)
 

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

Top