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

时间:2013-05-09 15:13:55

标签: .net wcf wcf-rest

我已经创建了一个宁静的WCF服务。每当客户端调用此服务方法时,我已经启动了一个具有一定间隔的Timer,如下所示

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);

2 个答案:

答案 0 :(得分:2)

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

尝试使用System.Threading.Timer。

答案 1 :(得分:0)

从此链接Timer Class

  

此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码具有可用的UI消息泵,并始终在同一个线程中运行,或者将调用编组到另一个线程上。

这意味着在这种情况下您无法使用System.Windows.Forms.Timer,它无法正常工作。