system.timers.timer不会在服务器上触发已发生的事件 [英] system.timers.timer doesn't fire the elapsed event on the server
问题描述
我有一个Web服务,我在其中使用了计时器。当我在我的vs2010中运行服务应用程序时,它的工作就像一个魅力。但是当我将它部署到远程服务器时,它不会触发ElapsedEventHandler。我使用Logger记录事件,并将写入权限设置为日志文件。让我解释一下。我正在尝试编写一份等待工作的服务,而这份工作正在向一个大清单发送电子邮件。和电子邮件应该以20秒的间隔发送。这是我的代码的一部分:
I have a Web Service and i used timers inside it. when i run the service application inside my vs2010 it work''s like a charm. but when i deploy it to the remote server it doesn''t fire the ElapsedEventHandler. I used a Logger to log the events and i set the permissions for write into the log file. Let me explain more. I''m trying to write a service which waits for a job, and the job is sending email to a big list. and emails should be send by 20 secs interval. here is some part of my code:
public class EmailSender : IEmailSender
{
Queue<AdvEmail> ListOfEmails = new Queue<AdvEmail>();
List<EmailJob> EmailJobs = new List<EmailJob>();
Timer timerFetchJobs = null;
Timer timerSendEmails = null;
public EmailSender()
{
try
{
Logger.Log("init service");
timerFetchJobs = new Timer();
timerFetchJobs.Interval = 5 * 1000;
timerFetchJobs.Elapsed += new ElapsedEventHandler(timerFetchJobs_Elapsed);
timerFetchJobs.Enabled = true;
timerSendEmails = new Timer();
timerSendEmails.Interval = 20 * 1000;
timerSendEmails.Elapsed += new ElapsedEventHandler(timerSendEmails_Elapsed);
timerSendEmails.Enabled = true;
timerFetchJobs.Start();
timerSendEmails.Start();
Logger.Log("init service Ended Succefully");
Logger.Log("timerFetchJobs.Enabled=" + timerFetchJobs.Enabled.ToString());
Logger.Log("timerSendEmails.Enabled=" + timerSendEmails.Enabled.ToString());
}
catch (Exception ex)
{
Logger.Log("Error in InitService. Message: " + ex.Message);
}
}
void timerSendEmails_Elapsed(object sender, ElapsedEventArgs e)
{
timerSendEmails.Stop();
Logger.Log("Checking for Emails in the Queue");
// some statments here
timerSendEmails.Start();
}
void timerFetchJobs_Elapsed(object sender, ElapsedEventArgs e)
{
timerFetchJobs.Stop();
try
{
FetchJobsFromDB();
}
catch (Exception ex)
{
Logger.Log("Error in timerFetchJobs. Message: " + ex.Message);
}
timerFetchJobs.Start();
}
当我从浏览器转到服务addreess时,它初始化并正确记录init级别,但没有解雇计时器的线索!我不知道为什么?我做错了什么?谢谢
编辑:这是我在本地运行时的日志文件:
日志条目:1392 / 01/30 08:47:09 :: init service -------------------------------
日志条目:1392/01/30 08:47:09 :: init服务结束成功--------------------- ----------
日志条目:1392/01/30 08:47:09 :: timerFetchJobs.Enabled = True ---- ---------------------------
日志条目:1392/01 / 30 08:47:09 :: timerSendEmails.Enabled = True -------------------------------
日志条目:1392/01/30 08:47:14 ::检查数据库中的工作------------------- ------------
日志条目:1392/01/30 08:47:16 ::有一份新工作。工作名称是Pad 71 -------------------------------
日志条目:1392/01/30 08:47:19 ::为工作添加电子邮件第71页-------------------------- -----
这是我将其部署到远程服务器时的日志文件
日志条目:1392/01/30 08:47:09 :: init service -------------------------------
日志条目:1392/01/30 08:47:09 :: init服务结束成功----------------- --------------
日志条目:1392/01/30 08:47:09 :: timerFetchJobs.Enabled = True -------------------------------
日志条目: 1392/01/30 08:47:09 :: timerSendEmails.Enabled = True -------------------------------
when i go to the service addreess from browser, it initializes and logs the init level correctly, but no clue of firing timers!! which i dont know why?? what I''m doing wrong? thanks
here is the log file when i run it locally:
Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------
Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------
Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------
Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------
Log Entry : 1392/01/30 08:47:14 : :Checking for Jobs in the Database-------------------------------
Log Entry : 1392/01/30 08:47:16 : :There is a new job. job name is Pad 71-------------------------------
Log Entry : 1392/01/30 08:47:19 : :Adding Emails for th job Pad 71-------------------------------
And here is the log file when i deploy it to remote server
Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------
Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------
Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------
Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------
推荐答案
System.Timers.Timer
有一个AutoReset
属性您应该设置为True
。
System.Timers.Timer
has aAutoReset
property which you should set toTrue
.
我更改了logger类以登录字符串变量并添加操作合同以获取日志一个d问题消失了!!!
现在我有另一个问题。过了一会儿,计时器停了!似乎没有新的请求,IIS关闭服务!我怎么能有一个不定时间服务?
I changed logger class to log in a string variable and add a operation contract to get the log and the problem gone!!!
now i have another problem. after a while the timers stop!! seems when there is no new request, IIS shuts down the service! how can i have a infinitive time service?
这篇关于system.timers.timer不会在服务器上触发已发生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!