Click here to Skip to main content
15,891,657 members
Home / Discussions / C#
   

C#

 
GeneralWindows Service: Timer vs Thread Pin
BBatts16-Jul-13 6:15
BBatts16-Jul-13 6:15 
GeneralRe: Windows Service: Timer vs Thread Pin
jschell16-Jul-13 8:18
jschell16-Jul-13 8:18 
GeneralRe: Windows Service: Timer vs Thread Pin
BBatts16-Jul-13 8:26
BBatts16-Jul-13 8:26 
GeneralRe: Windows Service: Timer vs Thread Pin
jschell17-Jul-13 8:40
jschell17-Jul-13 8:40 
GeneralRe: Windows Service: Timer vs Thread Pin
Richard MacCutchan16-Jul-13 20:30
mveRichard MacCutchan16-Jul-13 20:30 
GeneralRe: Windows Service: Timer vs Thread Pin
tasumisra17-Jul-13 0:27
tasumisra17-Jul-13 0:27 
GeneralRe: Windows Service: Timer vs Thread Pin
BBatts17-Jul-13 1:35
BBatts17-Jul-13 1:35 
GeneralRe: Windows Service: Timer vs Thread Pin
Nicholas Marty17-Jul-13 2:01
professionalNicholas Marty17-Jul-13 2:01 
I'd be using Timers for this purpose. As it's by far easier to control the execution of the timer instead of a thread.

I generally use the System.Threading.Timer with the following callback pattern:

Initialize it like that:
C#
// Declaration:
System.Threading.Timer _timer;

//Initialization:
_timer = new System.Threading.Timer(OnTimer, null, 10000, System.Threading.Timeout.Infinite);


And this callback method
C#
private void OnTimer(object state) {
   try {
      // Do your stuff here
   } catch (Exception ex) {
      // Errorhandling  
   } finally {
      // the first parameter is the delay until the callback method will be called again
      // the second parameter defines the interval in which it will be rais. Timeout.Infition -> Disabled
      _timer.Change(10000, System.Threading.Timeout.Infinite);
   }
}


To stop the timer just call:
C#
_timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);


If you use it like that it should never ever be called concurrently as after the first event the timer is disabled until you manually reenable it (i.e. in the finally clause)

Greetings
GeneralRe: Windows Service: Timer vs Thread Pin
BBatts17-Jul-13 2:07
BBatts17-Jul-13 2:07 
QuestionHow to create Dynamic MainMenu formation in WinForm Application using DataBase Pin
hoabanxanh15-Jul-13 23:47
hoabanxanh15-Jul-13 23:47 
SuggestionRe: How to create Dynamic MainMenu formation in WinForm Application using DataBase Pin
Richard MacCutchan15-Jul-13 23:54
mveRichard MacCutchan15-Jul-13 23:54 
AnswerRe: How to create Dynamic MainMenu formation in WinForm Application using DataBase Pin
Pete O'Hanlon15-Jul-13 23:54
mvePete O'Hanlon15-Jul-13 23:54 
GeneralRe: How to create Dynamic MainMenu formation in WinForm Application using DataBase Pin
hoabanxanh25-Jul-13 0:52
hoabanxanh25-Jul-13 0:52 
QuestionICursor Pin
jojoba2015-Jul-13 22:10
jojoba2015-Jul-13 22:10 
AnswerRe: ICursor Pin
Pete O'Hanlon15-Jul-13 22:41
mvePete O'Hanlon15-Jul-13 22:41 
QuestionRe: ICursor Pin
jojoba2015-Jul-13 22:51
jojoba2015-Jul-13 22:51 
AnswerRe: ICursor Pin
Pete O'Hanlon15-Jul-13 23:05
mvePete O'Hanlon15-Jul-13 23:05 
QuestionRe: ICursor Pin
jojoba2016-Jul-13 17:22
jojoba2016-Jul-13 17:22 
AnswerRe: ICursor Pin
Pete O'Hanlon16-Jul-13 20:10
mvePete O'Hanlon16-Jul-13 20:10 
QuestionRe: ICursor Pin
jojoba2016-Jul-13 20:19
jojoba2016-Jul-13 20:19 
AnswerRe: ICursor Pin
Mycroft Holmes16-Jul-13 20:22
professionalMycroft Holmes16-Jul-13 20:22 
Questionhello everybody i need some help to get the same in the following link (c# code and class and dynamically and based on stored procedure database) Pin
Member 184462915-Jul-13 8:12
Member 184462915-Jul-13 8:12 
AnswerRe: hello everybody i need some help to get the same in the following link (c# code and class and dynamically and based on stored procedure database) Pin
Dave Kreskowiak15-Jul-13 11:50
mveDave Kreskowiak15-Jul-13 11:50 
AnswerRe: hello everybody i need some help to get the same in the following link (c# code and class and dynamically and based on stored procedure database) Pin
Emmanuel Medina15-Jul-13 12:00
professionalEmmanuel Medina15-Jul-13 12:00 
GeneralRe: hello everybody i need some help to get the same in the following link (c# code and class and dynamically and based on stored procedure database) Pin
Member 184462915-Jul-13 22:59
Member 184462915-Jul-13 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.