如何像刷新眼睛exe每5分钟获得一条警报消息 [英] how to get a single alert message for every 5 minutes like refresh eyes exe

查看:87
本文介绍了如何像刷新眼睛exe每5分钟获得一条警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用Windows服务在c#中开发了一个应用程序,每2分钟就会收到一条警告信息。以下是我的代码。



我的代码出现问题的是每隔两分钟发出一条警报消息,而不是一条警报消息。我怎么能只限制一条警报消息。



提前致谢。

Hi

I developed an application in c# using windows services to get a alert message for every 2 mintues .following is my code.

The problem with my code is many alert messages are coming instead of one alert message for every two minutes.How can I restrict to only one alert message.

Thanks in advance.

protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

//This statement is used to set interval to 1 minute (= 60,000 milliseconds)

timer.Interval = 60000*2;

//enabling the timer
timer.Enabled = true;
}

protected override void OnStop()
{
timer.Enabled = false;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
while (true)
{
MessageBox.Show("hello there!");
System.Threading.Thread.Sleep(60 * 2000);
}
}

推荐答案

你的问题不明显吗?你的事件处理程序方法已经被假定每两分钟被定期调用一次,但是另外,你也会用2分钟的睡眠进行这个循环,顺便说一下,这完全阻塞了调用线程,造成了不可能的混乱。我甚至无法想象什么推理可能导致这种奇怪的代码。仅有两个立即常量具有相同值的事实 60000 * 2 60 * 2000 给你一个提示?只需删除此循环。



然后,您将每两分钟弹出一次模态对话框。如果用户没有一直关闭它们,它们将叠加在顶部,越来越多,最终耗尽所有系统资源。整个想法不值得考虑。更好地考虑做一些有用的事情。



-SA
Isn't your problem obvious? Your event handler method is already supposed to be called periodically each two minuted, but additionally, you also do this loop with 2-minute sleep, which, by the way, totally blocks the calling thread, creating impossible mess. I cannot even imagine what reasoning could possibly lead to such weird code. Wouldn't the mere fact having of two immediate constants with the same values 60000*2 and 60 * 2000 give you a hint? Just remove this loop.

But then, you will have modal dialog box popping up every two minutes. If the user is not closing them all the time, they will stack up on top, more and more of them, eventually depleting all the system resource. The whole idea does not deserve consideration. Better think of doing something useful.

—SA


这篇关于如何像刷新眼睛exe每5分钟获得一条警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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