473,699 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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 2246
I believe you need a System.Timers.T imer 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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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**********@g mail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>I believe you need a System.Timers.T imer 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.Enabl ed = true;
timer1.Interva l = 1500;

and on my timer_tick i set this.

int intHour = DateTime.Now.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadi ng.Thread.Sleep (3000);
SendMessage((i nt)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadi ng.Thread.Sleep (10000);
SendMessage((i nt)-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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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.T imer 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**********@g mail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
I believe you need a System.Timers.T imer 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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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*******@yaho o.nospammin.com wrote in message
news:F4******** *************** ***********@mic rosoft.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.Enabl ed = true;
timer1.Interva l = 1500;

and on my timer_tick i set this.

int intHour = DateTime.Now.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadi ng.Thread.Sleep (3000);
SendMessage((i nt)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadi ng.Thread.Sleep (10000);
SendMessage((i nt)-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**********@g mail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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.T imer 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.go oglegroups.com. ..
>I believe you need a System.Timers.T imer 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.Enabl ed = true;
timer1.Interva l = 1500;

and on my timer_tick i set this.

int intHour = DateTime.Now.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadi ng.Thread.Sleep (3000);
SendMessage((i nt)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadi ng.Thread.Sleep (10000);
SendMessage((i nt)-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**********@g mail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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.T imer 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**********@g mail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
I believe you need a System.Timers.T imer 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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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 ElapsedEventHan dler(OnElapsed) ;

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

void OnElapsed(Objec t sender, ElapsedEventArg s 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******** ************@fe 05.news.easynew s.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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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 ElapsedEventHan dler(OnElapsed) ;

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

void OnElapsed(Objec t sender, ElapsedEventArg s 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******** ************@fe 05.news.easynew s.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.Ho ur;

if (intHour < 7)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else if (intHour 20)
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
else
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_ON);
but it dosnt work.

i tried setting

System.Threadin g.Thread.Sleep( 3000);
SendMessage((in t)-1, WM_SYSCOMMAND, SC_MONITORPOWER , MONITOR_OFF);
System.Threadin g.Thread.Sleep( 10000);
SendMessage((in t)-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

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

Similar topics

2
15689
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. Thanks Andrew
5
5995
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 appears to stop. any help is greatly appreciated...
2
1215
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 LocalSystem account, I have set the interval to 2000 (2 seconds) in the OnStart event (which I know is firing because I can debug it and I've event logged it). There seems to be no exceptions... the timer is linked up to its tick event correctly. What...
8
3337
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 which represents a clock on a form, to which the original class has a reference. (MVC style) If the user clicks and holds the X button on the form
0
1652
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 "for(i=0;i<=3;i++)" where I set "myTimer = new Timer;" and the Eventhandler "myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);"
2
2474
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 in a service: the Timer::Tick event processor never gets called. We have set the timer interval, enabled it, and started it, but still nothing. In my case, I also have enabled an event handler to detect SessionSwitch, but that never gets...
10
6469
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 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
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...
2
1417
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 initializecomponent() OnStart () enables the timer1 and OnStop() disables the timer1. Can anybody help?
2
1768
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 again enable after sucessfully return.but my code is making exception and not throwing exception then how could i restart service automatically.
0
9171
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
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
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
6532
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
4373
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...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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
2008
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.