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

.net System.Windows.Forms.Timer在我的WCF服务中不工作,.net,wcf,wcf-rest,.net,Wcf,Wcf Rest,我已经创建了一个restful WCF服务。每当客户端调用这个服务方法时,我都会启动一个计时器,时间间隔如下 var timer = new Timer(); timer.Interval = Convert.ToInt32(attempt); timer.Tick += new EventHandler(timer_Tick); var tempAttempt = new TempAttempt { AlertInformationId = currentAlertId, At

我已经创建了一个restful 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。

从此链接

此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码有一个可用的UI消息泵,并且总是从同一个线程运行,或者将调用封送到另一个线程


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

在我的服务中,我需要创建多个计时器,还需要保存计时器id。在System.Windows.timers中,有一个名为Tag的属性用于设置名称。在system.Threading.Timer中,如何设置用于保存的唯一名称?可以在构造函数中使用对象状态。公共计时器(TimerCallback回调、对象状态、int dueTime、int period)您的TimerCallback委托将对象作为参数获取。