首页 / C# / c#-轮询Windows服务的计时器
c#-轮询Windows服务的计时器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-轮询Windows服务的计时器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3595字,纯文字阅读大概需要6分钟。
内容图文
![c#-轮询Windows服务的计时器](/upload/InfoBanner/zyjiaocheng/949/c3d89d0bf4fb49f794876670c7fc70b7.jpg)
我编写了一个Timer类,以便在轮询另一个系统的Windows服务中使用它.我这样做是因为有两个System.Timers.Timer无法解决的问题.
> Elapsed EventHanler在后台运行,因此如果主线程结束,其执行将中止.我希望System.Timers.Timer.Stop函数阻塞主线程,直到Elapsed EventHanler的执行结束为??止.
> System.Timers.Timer不处理事件重入.我希望时间间隔在两个Elapsed EventHanler之间,以便如果以前的调用(时间间隔)尚未完成,则计时器永远不会调用Elapsed EventHanler.
在编写该类时,我发现我需要解决一些棘手的相关问题,并且由于我不太熟悉这些问题,所以我想知道以下Timer类是否是线程安全的?
public class Timer
{
System.Timers.Timer timer = new System.Timers.Timer() { AutoReset = false };
ManualResetEvent busy = new ManualResetEvent(true);
public double Interval
{
get { return timer.Interval; }
set { timer.Interval = value; }
}
public Timer()
{
timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
}
void TimerElapsed(object sender, ElapsedEventArgs e)
{
try
{
busy.Reset();
OnElapsed(e);
timer.Start();
}
finally
{
busy.Set();
}
}
public event EventHandler Elapsed;
protected void OnElapsed(EventArgs e)
{
if (Elapsed != null)
{
Elapsed(this, e);
}
}
public virtual void Start()
{
busy.WaitOne();
timer.Start();
}
public virtual void Stop()
{
busy.WaitOne();
timer.Stop();
}
}
解决方法:
首先,根据我的经验,您可以使用System.Threading.Timer代替此计时器,根据我的经验,这是性能更好的计时器(只是有关个人经验的建议).
其次,在这种情况下,您应该提供一个标志,该标志在较早的计时器完成任务后立即设置(此标志-所有线程都可以访问的静态字段).
在这种情况下,请确保在发生任何错误的情况下,还重置了该标志,以使另一个计时器不会无限期地等待,以防计时器任务由于内部发生错误而无法为其他计时器设置该标志.任务(应添加最终块的种类,以确保处理错误并始终重置标志).
重置此标志后,下一个线程将对其进行处理,因此此检查将确保所有线程在同一线程上启动任务.
我针对这种情况编写的示例代码(方法代码已删除,这将为您提供设计详细信息).
namespace SMSPicker
{
public partial class SMSPicker : ServiceBase{
SendSMS smsClass;
AutoResetEvent autoEvent;
TimerCallback timerCallBack;
Timer timerThread;
public SMSPicker()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
smsClass = new SendSMS();
autoEvent = new AutoResetEvent(false);
long timePeriod = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["timerDuration"]) ? 10000 : Convert.ToInt64(ConfigurationSettings.AppSettings["timerDuration"]);
timerCallBack = new TimerCallback(sendSMS);
timerThread = new Timer(timerCallBack, autoEvent, 0, timePeriod);
}
private void sendSMS(object stateInfo)
{
AutoResetEvent autoResetEvent = (AutoResetEvent)stateInfo;
smsClass.startSendingMessage();
autoResetEvent.Set();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
smsClass.stopSendingMessage();
timerThread.Dispose();
}
}
}
namespace SMSPicker
{
class SendSMS
{
//This variable has been done in order to ensure that other thread does not work till this thread ends
bool taskDone = true;
public SendSMS()
{
}
//this method will start sending the messages by hitting the database
public void startSendingMessage()
{
if (!taskDone)
{
writeToLog("A Thread was already working on the same Priority.");
return;
}
try
{
}
catch (Exception ex)
{
writeToLog(ex.Message);
}
finally
{
taskDone = stopSendingMessage();
//this will ensure that till the database update is not fine till then, it will not leave trying to update the DB
while (!taskDone)//infinite looop will fire to ensure that the database is updated in every case
{
taskDone = stopSendingMessage();
}
}
}
public bool stopSendingMessage()
{
bool smsFlagUpdated = true;
try
{
}
catch (Exception ex)
{
writeToLog(ex.Message);
}
return smsFlagUpdated;
}
}
}
内容总结
以上是互联网集市为您收集整理的c#-轮询Windows服务的计时器全部内容,希望文章能够帮你解决c#-轮询Windows服务的计时器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。