Posted 9-Apr am Biinny. Add a Solution. Accept Solution Reject Solution. The main problem of using an infinite loop is that your service can't be stopped. A proper implementation would do the work within a thread where the loop can be left upon a stop event. It is also necessary that the startup function of a Windows service returns in a timely fashion. Use WaitHandle.
WaitOne Method Int32 System. Using the timeout parameter you can avoid using a timer and calling Sleep. Posted 9-Apr am Jochen Arndt. Add your solution here. When I click both of these buttons, the message boxes will be displayed after 5 seconds. But there are significant differences between these two implementations. This is the classic way of suspending execution. This method will suspend the current thread until the given amount of time has elapsed.
When you call Thread. Sleep in the above way, there is nothing you can do to abort this except waiting until the time elapses or by restarting the application. Sleep suspends the thread that's making the call. And because I'm calling Thread. Sleep in my button event handler running in the UI thread , the UI is not responsive while it's sleeping.
Delay acts in a very different way than Thread. Follow Post Reply. Brendan Grant. Unfortunately, most timers like System. Sleep are not very precise as the time between triggers is dependant on a lot of things, so having exact intervals can be extremely difficult.
Using timers, I would suggest dramatically decreasing the time and implement some sort of check like checking the current time against time that your worker function was last run which you would have to note.
An improvement on this would be to move away from the timer completely and implement it with a thread where you have a main loop, always looping with the same logic as mentioned above, only if the condition is not met, a Sleep call to cause the thread to sleep for a short period of time a matter of seconds ideally before checking again. That is true, but timers are can be trusted to be accurate within fractions of a second, and certainly to not be out by seconds, or even minutes, as indicated in the OP.
I'd only hesitate to use a timer if the required accuracy was greater than ms and there are timers for getting better accuracy than that. An error of 8 minutes is not due to the timers, but either the program logic, or some deadly disturbance in the system. I wrote this little console program to test the accuracy of System. The first run, I set the timer interval to ie. I did a second run, with an interval of 5 minutes, and the accuracy was the same. Now - startTime; Console.
This discussion thread is closed Start new discussion. Enabled to false , either of which will stop the timer from running. Change tickInterval , Timeout. Dispose waitHandle ; waitHandle. The documentation for the Dispose overload I'm using says: The timer is not disposed until all currently queued callbacks have completed. Generic ; using System. Linq ; using System.
Text ; using System. Tasks ; using System.
0コメント