Timer in Java Thread

A few errors in your code snippet: You extend the Thread class, which is not really good practice You have a Timer within a Thread? That doesnt make sense as the a Timer runs on its own Thread. You should rather (when/where necessary), implement a Runnable see here for a short example, however I cannot … Read more

Windows Service to run a function at specified time

(1) On first start, Set _timer.Interval to the amount of milliseconds between the service start and schedule time. This sample set schedule time to 7:00 a.m. as _scheduleTime = DateTime.Today.AddDays(1).AddHours(7); (2) On Timer_Elapsed, reset _timer.Interval to 24 hours (in milliseconds) if current interval is not 24 hours. System.Timers.Timer _timer; DateTime _scheduleTime; public WinService() { InitializeComponent(); … Read more

How I can run my TimerTask everyday 2 PM?

Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 2); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); // every night at 2am you run your task Timer timer = new Timer(); timer.schedule(new YourTask(), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day

Android Timer schedule vs scheduleAtFixedRate

The difference is best explained by this non-Android documentation: Fixed-rate timers (scheduleAtFixedRate()) are based on the starting time (so each iteration will execute at startTime + iterationNumber * delayTime). In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such … Read more

How do I pass an object into a timer event?

The easiest way to do this is to change the event handler into an anonymous function. It allows you to pass the string at the point of declaration. string theString = …; timer.Elapsed += (sender, e) => MyElapsedMethod(sender, e, theString); static void MyElapsedMethod(object sender, ElapsedEventArgs e, string theString) { … }