Windows Service速度问题 [英] Windows Service speed problem
本文介绍了Windows Service速度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Windows Service中的两个计时器有疑问
第二个TimerLimit永远不会执行.
我已经设置了第一个计时器来解析电子邮件,而我需要它不断地进行操作,而第二个计时器是因为如果阅读邮件后发生了某些事情,我需要在20秒后执行.
我是初学者,但似乎有冲突.
是否有可能因为第一次计时器太快?
有什么解决这个问题的建议吗?
预先谢谢你
I have a problem regarding two timers in Windows Service
The second TimerLimit is never executed.
I have set up first timer to parse email and i need it to do constantly while the second timer is because i need to be executed after 20 seconds if something happen after having reading emails.
I am a beginner but it seems like there is a conflict.
Is it possible because the first timer is too fast ?
any suggestion to solve this problem ?
Thank you in advance
public partial class GSAService : ServiceBase
{
Timer timer;
Timer timerLimit;
bool stop = false;
public GSAService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
stop = false;
ParseEmail.DeleteFiles();
timer = new Timer(200);
timer.Elapsed += new ElapsedEventHandler(ExecuteService);
timer.Enabled = true;
timerLimit = new Timer(20000);
timerLimit.Elapsed += new ElapsedEventHandler(ExecuteServiceLimit);
timerLimit.Enabled = true;
}
protected override void OnStop()
{
stop = true;
}
private void ExecuteService(object source, ElapsedEventArgs e)
{
if (stop)
{
return;
}
try
{
if (ParseEmail.isNotUse)
{
ParseEmail.isNotUse = false;
ParseEmail.ParseValues();
}
}
catch (Exception exp)
{
ParseEmail.isNotUse = true;
Console.WriteLine("Exception: {0}", exp.ToString());
}
}
private void ExecuteServiceLimit(object source, ElapsedEventArgs e)
{
if (stop)
{
return;
}
try
{
ParseEmail.CheckLimitOrders();
}
catch (Exception exp)
{
Console.WriteLine("Exception: {0}", exp.ToString());
}
}
}
}
推荐答案
使用线程代替使用计时器.
编辑============================
例如,请转到本文页面:
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=240517 [^ ]
查找ReputationScraper
,然后打开文件RepScraperSvc.cs文件.它说明了一个调度线程.您将需要其中两个.至于让线程为您服务,google是您的朋友,CP上有很多关于它的文章.
Instead of using Timers, use Threads.
EDIT =============================
For an example, go to this article page:
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=240517[^]
Look for theReputationScraper
, and open the file RepScraperSvc.cs file. It illustrates a scheduling thread. You will need two of them. As far as making threading work for you, google is your friend, and there are a lot of articles here on CP about it.
这篇关于Windows Service速度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文