473,748 Members | 4,178 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 2249
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
15695
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
5998
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
1217
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
3343
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
1656
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
2482
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
6477
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
12245
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
1419
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
1769
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
8987
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
8826
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
9534
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
9366
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
9316
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,...
1
6793
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
6073
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
4597
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
3303
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

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.