473,405 Members | 2,354 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,405 software developers and data experts.

Timer not working on my Service.

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

Jul 27 '06 #1
10 2223
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.
janhm wrote:
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
Jul 27 '06 #2
Thanks Robb.

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

/Jan
<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>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.
janhm wrote:
>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

Jul 27 '06 #3
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"janhm" wrote:
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

Jul 27 '06 #4
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.
janhm wrote:
Thanks Robb.

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

/Jan
<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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.
janhm wrote:
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
Jul 27 '06 #5
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

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:F4**********************************@microsof t.com...
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"janhm" wrote:
>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


Jul 27 '06 #6
Strange.. I checked it twice.. Im using vs 2005

<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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.
janhm wrote:
>Thanks Robb.

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

/Jan
<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googleg roups.com...
>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.
janhm wrote:
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

Jul 27 '06 #7
Ah, 2k3 over here. :(
janhm wrote:
Strange.. I checked it twice.. Im using vs 2005

<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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.
janhm wrote:
Thanks Robb.

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

/Jan
<Ro**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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.
janhm wrote:
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
Jul 27 '06 #8
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

"janhm" <no******@this.adrwrote in message
news:rF********************@fe05.news.easynews.com ...
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



Jul 27 '06 #9
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.
janhm wrote:
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

"janhm" <no******@this.adrwrote in message
news:rF********************@fe05.news.easynews.com ...
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

Jul 27 '06 #10

<Ro**********@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
>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 :)
Jul 27 '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. ...
5
by: caulker | last post by:
I have a simple Windows Service with a timer (system.timer). The service seems to run just fine when I have the Timer = 1 minute, but when I set the Timer = 15 minutes it only fires once ... then...
2
by: Beeeeeeeeeeeeves | last post by:
Hi I have created a windowds service project, it is working fine and I can debug it ok, but there is one thing up - when I drop a timer onto it, it doesn't work. I have set the service to be a...
8
by: theinvisibleGhost | last post by:
I think I've found a bug in the timer control. I've got a class which uses a timer control. It imports it from System.Windows.Forms This timer ticks once a second, and then updates a label...
0
by: Björn | last post by:
Hi, I have problems using the timer object in a C# Windows Service In my OnStart() Method of the Windows Service I declare a timer object "Timer myTimer = 0;" Than I have a loop...
2
by: r norman | last post by:
Please excuse the cross-posting. This question was raised in microsoft.public.dotnet.general but hasn't been answered so I am trying where I can. There are two of us who have the same problem...
10
by: hazz | last post by:
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...
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...
2
by: dhulapati | last post by:
I cant get the timer working with the windows service. I have the timer set to 3600 ms. It polls only for Start and Stop but not at intervals. The timer1.tick event handler is called at the...
2
by: amitsingh | last post by:
in window service if service code is making exception and not comming back to restart the service again.because i am using timer enable service. i have set timer as disable after calling code and set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.