1

I have created a restful WCF service.Whenever client is calling this service method i have started a Timer with certain interval like below

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

After starting the timer the tick is not happened after that particular interval.How can i resolve this?

My Service

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

2 回答 2

2

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

尝试 System.Threading.Timer 代替。

于 2013-05-09T15:19:17.630 回答
0

从此链接Timer Class

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

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

于 2013-05-09T16:41:49.730 回答