System.Windows.Forms.Timer是不是在我的WCF服务工作

问题描述:

我创建了一个宁静的WCF service.Whenever客户端调用该服务的方法我已经开始用一定的时间间隔定时器像下面System.Windows.Forms.Timer是不是在我的WCF服务工作

var timer = new Timer(); 
timer.Interval = Convert.ToInt32(attempt); 
timer.Tick += new EventHandler(timer_Tick); 
var tempAttempt = new TempAttempt 
{ 
    AlertInformationId = currentAlertId, 
    Attempt = GetAttemptId(attempt), 
    Status = false, 
    TimerId = "Timer" + currentAlertId.ToString() 
}; 

if (CreateNewAttempt(tempAttempt)) 
{ 
    timer.Tag = tempAttempt.TimerId; 
    timer.Enabled = true; 
    timer.Start(); 
} 

private void timer_Tick(object sender, EventArgs e) 
{ 
    //blah blah   
    Timer t = (Timer)sender; 
} 

后启动计时器后,在特定时间间隔之后不会发生滴答。如何解决此问题?

我的服务

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/PushNotification")] 
[OperationContract] 
void PushNotification(MailInformation mailInformations); 

System.Windows.Forms.Timer是服务错误的类。

改为尝试System.Threading.Timer。

+0

在我的服务我需要创建多个计时器,我也需要保存计时器ID。在System.Windows.Timers中有一个名为Tag的属性来设置名称。在system.Threading.Timer中,我如何设置一个唯一的名称来保存? – JEMI 2013-05-09 15:23:08

+1

您在构造函数中使用Object状态。 公共定时器( \t TimerCallback回调, \t对象状态, \t INT duetime参数, \t INT期 ) 您TimerCallback委托方获取对象作为参数。 – 2013-05-09 15:38:08

从这个链路Timer Class

此Windows定时器被设计用于其中UI线程用于执行处理单线程环境。它要求用户代码具有可用的UI消息泵,并始终从同一线程运行,或将该调用编组到另一个线程。

这意味着你不能在这种情况下使用System.Windows.Forms.Timer,它不会工作。