Jameel Moideen
Jameel Moideen

Reputation: 7931

System.Windows.Forms.Timer is not Working in my WCF service

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

Upvotes: 1

Views: 321

Answers (2)

Enes
Enes

Reputation: 1125

From this linkTimer Class

This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

Meaning that you cannot use System.Windows.Forms.Timer in this case, it wont work.

Upvotes: 0

Mirco Ellmann
Mirco Ellmann

Reputation: 968

System.Windows.Forms.Timer is the wrong class for a service.

try System.Threading.Timer instead.

Upvotes: 2

Related Questions