计时器无法使用我的服务。 [英] Timer not working on my Service.
问题描述
你好。
我创建了一个Windows服务,应该在一天的特定时间打开/关闭显示器
,但我的计时器似乎没有工作。
在我的服务开始我设置:
timer1.Enabled = true;
timer1.Interval = 1500;
和我的timer_tick我设置了这个。
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);
但是它的工作量很大。
i尝试设置
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);
服务启动,这个工作正常,所以我知道我的Sendmessage有效。
有什么建议吗?
/ Jan
我相信你需要一个支持Timer_Elapsed的System.Timers.Timer
事件。您正在使用一个使用
Timer_Tick事件的System.Windows.Forms.Timer。我相信你想要的计时器在工具箱的组件
部分。
janhm写道:
您好。
我创建了一个Windows服务,应该在一天的特定时间打开/关闭显示器
,但我的计时器dos'''好像有效。
我的服务开始我设置:
timer1.Enabled = true;
timer1.Interval = 1500;
和我的timer_tick我设置了这个。
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);
但它的工作量很大。
i尝试设置
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);
服务启动,这很好用,所以我知道我的Sendmessage有效。
有什么建议吗?
/ Jan
谢谢Robb。
我明白了你的意思,但是我确实使用了工具箱中
的组件部分。
/ Jan
< Ro ********** @ gmail.comwrote in message
news:11 ************* *********@p79g2000cwp.googlegr oups.com ...
>我相信你需要一个支持Timer_Elapsed的System.Timers.Timer
活动。您正在使用一个使用
Timer_Tick事件的System.Windows.Forms.Timer。我相信你想要的计时器在工具箱的组件
部分。
janhm写道:
>您好。
我已经创建了一个Windows服务,应该在一天的特定时间打开/关闭显示器
,但我的计时器似乎不是在我的服务开始工作。
timer1.Enabled = true;
timer1.Interval = 1500;
在我的timer_tick上我设置了这个。
int intHour = DateTime.Now.Hour;
if(intHour< 7)
SendMessage((int) - 1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_OFF);
否则if(intHour 20)
SendMessage((int)-1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_OFF);
其他
SendMessage(( int)-1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_ON);
但是dosnt工作。
我试过设置
System.Threading.Thread。睡眠(3000);
SendMessage((int)-1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_OFF);
系统。 Threading.Shread.Sleep(10000);
SendMessage((int)-1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_ON);
关于Service Start,这很好用,所以我知道我的Sendmessage工作。
任何建议?
/ Jan
janhm,
Windows服务似乎是一个非常大的开销,只需要一些真正需要每天执行2X的
。
为什么不只是将其转换为控制台应用程序并将其设置为在任务计划程序中的适当时间运行
?
Peter
-
联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
" janhm"写道:
你好。
我创建了一个Windows服务,可以打开/关闭显示器/>
在一天的特定时间,但我的计时器似乎无法正常工作。
在我的服务开始我设置:
timer1.Enabled = true;
timer1.Interval = 1500;
和我的timer_tick我设置了这个。
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);
但是它没有用。
>
i尝试设置
System.Threading.Thread.Sleep(3000);
SendMessage((int)-1,WM_SYSCOMMAND,SC_MONITORPOWER, MONITOR_OFF);
System.Thr eading.Thread.Sleep(10000);
SendMessage((int)-1,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_ON);
在Service Start上,这个工作正常,所以我知道我的Sendmessage有效。
有什么建议吗?
/ Jan
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
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
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
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
这篇关于计时器无法使用我的服务。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!