How might I schedule a C# Windows Service to perform a task daily?

I wouldn’t use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since the last run: private Timer _timer; private DateTime _lastRun = DateTime.Now.AddDays(-1); protected override void OnStart(string[] args) { _timer = … Read more

Scheduling a job with Spring programmatically (with fixedRate set dynamically)

Using a Trigger you can calculate the next execution time on the fly. Something like this should do the trick (adapted from the Javadoc for @EnableScheduling): @Configuration @EnableScheduling public class MyAppConfig implements SchedulingConfigurer { @Autowired Environment env; @Bean public MyBean myBean() { return new MyBean(); } @Bean(destroyMethod = “shutdown”) public Executor taskExecutor() { return Executors.newScheduledThreadPool(100); … Read more

Does spring @Scheduled annotated methods runs on different threads?

For completeness, code below shows the simplest possible way to configure scheduler with java config: @Configuration @EnableScheduling public class SpringConfiguration { @Bean(destroyMethod = “shutdown”) public Executor taskScheduler() { return Executors.newScheduledThreadPool(5); } … When more control is desired, a @Configuration class may implement SchedulingConfigurer.

How to conditionally enable or disable scheduled jobs in Spring?

The most efficient way to disable @Scheduled in Spring is to set cron expression to – @Scheduled(cron = “-“) public void autoEvictAllCache() { LOGGER.info(“Refresing the Cache Start :: ” + new Date()); activeMQUtility.sendToTopicCacheEviction(“ALL”); LOGGER.info(“Refresing the Cache Complete :: ” + new Date()); } From the docs: CRON_DISABLED public static final String CRON_DISABLED A special cron … Read more

How to check if a service is running via batch file and start it, if it is not running?

To check a service’s state, use sc query <SERVICE_NAME>. For if blocks in batch files, check the documentation. The following code will check the status of the service MyServiceName and start it if it is not running (the if block will be executed if the service is not running): for /F “tokens=3 delims=: ” %%H … Read more