Take the 2-minute tour ×

In C#, what is the best way to create a polling mechanism? So I have some code which can periodically do some action.

This is for web services so any UI-related solutions won't apply.


share|improve this question
add comment

5 Answers

up vote 1 down vote accepted

If you are saying that you have a web service which is supposed to periodically take some action on it's own, then I think you haven't quite got the web services model. Web services are supposed to sit there (just like a web page) until something kicks it off.

Otherwise you are dealing with a very brittle situation where anything could cause it to just stop.

If you have a bit of code that needs to run on a timer, then you should investigate placing that code in a windows service (not to be confused with Web Service). That's what they are for.

share|improve this answer
Moving the code to a different service is not necessarily a good plan. Consider using them together: Let the Windows service operate in the background with a timer that makes a request against the web service. Then all the code working with the web service's data is still contained within the web application. –  Ben Voigt Jul 13 '10 at 1:22
@Ben Voigt: I'd agree with that in order to keep your logic exactly where it needs to be: in the web service and therefor callable by other systems. So I guess the agreement is that something outside of the web service needs to be responsible for maintaining when it's time to run and causing the execution. –  Chris Lively Jul 13 '10 at 15:32
Yea a windows service would be best. This is hypothetical but something in some programming triggered a thought process in me. When I said web services, what I really mean is at the business logic layer, not in any code behind for a UI. –  dotnetdev Jul 14 '10 at 20:06
add comment

"code which can periodically do some action" is called a "Timer". Search MSDN, you'll find three or four classes for the purpose, several of which are non-gui (System.Threading.Timer comes to mind).

EDIT: To whom do the changes need to be visible? If they are only visible to other consumers of the web service, then one approach is for each incoming request can check whether a periodic action is overdue and perform it. However, you shouldn't do things this way if (1) the changes need to be visible to e.g. other clients of the same database, or (2) the periodic changes need greater permissions than arbitrary incoming requests. Also, the periodic actions might be interrupted if the client cancels their request, and doing the actions might significantly delay the response to the client. For these reasons I don't recommend adding periodic processing to normal request/response processing.

share|improve this answer
add comment

Take a look at the System.Threading.Timer class. This makes periodic calls to a method you supply. These calls are made on a separate thread (a thread in the thread pool).

share|improve this answer
add comment

You'll want to use a timer.

There are a few timers in the .NET framework, including:

  • System.Timers.Timer
  • System.Threading.Timer
  • System.Windows.Forms.Timer

See this article for help choosing the right one: Comparing the Timer Classes in the .NET Framework Class Library

share|improve this answer
Yea it'd be the timers.timer which I need. –  dotnetdev Jul 14 '10 at 20:04
add comment

It sounds like you want a Timer, but that does not make sense to use in a Web service. It would make more sense if the timer was in a client. What clients use your web service?

share|improve this answer
add comment

Your Answer


