带定时器问题的Windows服务 [英] Windows Service with Timer Question

查看:54
本文介绍了带定时器问题的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经提供了一个启动计时器的服务。我有

另一个停止并启动服务的Windows窗体应用程序。请

我需要在服务停止时处理任何已启动的计时器吗?作为这个服务的新东西,我不知道它是否有状态或记忆

我需要停止所有活跃的计时器或者如果神奇的gc会来

并且帮忙。


谢谢,我希望这是有道理的。


祝你有美好的一天,

Ryan

解决方案

Ryan写道:


大家好,


我已经提供了一个启动计时器的服务。我有

另一个停止并启动服务的Windows窗体应用程序。请

我需要在服务停止时处理任何已启动的计时器吗?作为这个服务的新东西,我不知道它是否有状态或记忆

我需要停止所有活跃的计时器或者如果神奇的gc会来

并且帮忙。


谢谢,我希望这是有道理的。



In你的情况在这里,如果计时器是在父线程上启动的,那么

会启动服务,计时器将被杀死

当服务停止时。但是,如果计时器在

生成的子线程中实例化,即使你已经停止服务,你也不会杀死该线程,那么子线程和计时器仍然会运行
.


魔法GC会在服务运行时杀死计时器,如果

你' '不小心,GC.Keepalive会阻止它。


在.Net服务中编写定时执行的正确方法是使用

线程一个定时的基础,在一个真实的循环中使用Tread.Sleep。


我从来没有听说过....我总是告诉人们和MS人员使用

Threading.Timer获取服务并始终避免使用while循环


" DArnold" < DA ***** @ DArnold.com写信息

新闻:eR ************* @ TK2MSFTNGP06.phx.gbl ...


Ryan写道:


>大家好,

我已经提供了服务它会在启动时启动计时器。我有另一个窗体应用程序停止并启动服务。当服务停止时,我需要处理任何已启动的计时器吗?作为这项服务的新手,我不知道它是否具有状态或记忆
我需要停止所有活跃的计时器,或者如果神奇的gc将来到并帮助。

谢谢,我希望这是有道理的。



在这种情况下,如果定时器是在父线程上启动的,那么

开始启动服务,当服务停止时,定时器将被杀死

。但是,如果计时器在

生成的子线程中实例化,即使你已经停止服务,你也不会杀死该线程,那么子线程和计时器仍然会运行



如果你是
$ b,神奇的GC会在服务运行时杀死计时器$ b不小心,GC.Keepalive会阻止它。


在.Net服务中编写定时执行的正确方法是使用

线程一个定时的基础,在一个真实的循环中使用Tread.Sleep。


blockquote>




" Smokey Grindle" < no **** @ nospam.comwrote in message

news:%2 ***************** @ TK2MSFTNGP02.phx.gbl .. 。


>我从来没有听说过....人们和MS人总是告诉我们使用Threading.Timer进行服务和总是避免while循环
这个



什么MS人?当我第一次使用

框架1.1开始在VS 2003上编写.Net服务时,我工作的一个人质疑我为什么使用

而True循环。我告诉他它来自VS帮助进行线程化。


然后他回来了一篇关于Whlie True循环的文章是

首选方法在.NET服务中使用Thread.Sleep。我一直在寻找

的链接,但我找不到它。


当我编写.NET服务或控制台应用程序时,我会去使用具有While True循环的委托生成子线程,作为工作程序

线程或线程。我将使用的唯一计时器是检查线程状态。


公共类StartThread()

{


尝试

{

而真实

{

做点什么

Thread.Sleep (timetowait)

}

}

Catch ThreadException为tex //如果它遇到Catch,那么它即将来临
离开循环

{

做点什么

}

}


Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don''t know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

Have a great day,
Ryan

解决方案

Ryan wrote:

Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don''t know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.

In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don''t kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if
you''re not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.


I have NEVER heard that.... I was always told by people and MS people to use
the Threading.Timer for services and to always avoid while loops for this

"DArnold" <DA*****@DArnold.comwrote in message
news:eR*************@TK2MSFTNGP06.phx.gbl...

Ryan wrote:

>Hello everyone,

I have made a service that starts timers when it starts. I have
another windows form application that stops and starts the service. Do
I need to deal with any started timers when the service stops? Being
new at this service stuff, I don''t know if it holds state or memory
and I need to stop all active timers or if the magical gc will come
along and help out.

Thanks, I hope that makes sense.


In your case here, if the timers were started on the parent thread, the
tread the service started up on, the the timers are going to be killed
when the service stops. However, if the timer was instantiated on a
spawned child thread and you don''t kill the thread even though you have
stopped the service, then the child thread and the timer will still be
running.

The magical GC will kill the timer while the service is running if you''re
not careful, GC.Keepalive will prevent that.

The proper way to write a timed execution in a .Net service is to use
threads on a timed basis, using the Tread.Sleep in a While True Loop.





"Smokey Grindle" <no****@nospam.comwrote in message
news:%2*****************@TK2MSFTNGP02.phx.gbl...

>I have NEVER heard that.... I was always told by people and MS people to
use the Threading.Timer for services and to always avoid while loops for
this

What MS people? When I first started writing .Net services on VS 2003 using
the Framework 1.1, a guy I worked for questioned me about why I was using a
While True loop. I told him it came right out of the VS Help for threading.

Then he came back with an article about the Whlie True loop was the
preferred method using Thread.Sleep in a .NET Service. I have been looking
for that link, but I cannot find it.

When I am writing a .NET Service or Console application, then I am going to
spawn a child thread using a delegate with a While True loop, as the worker
thread or threads. The only Timer I will use is to check thread state.

public class StartThread()
{

try
{
while true
{
do something
Thread.Sleep(timetowait)
}
}
Catch ThreadException as tex //if it hits the Catch, then it''s coming
out of the loop
{
do something
}
}


这篇关于带定时器问题的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆