There is an application written in C #, all the parameters of the application are registered in the config.ini. I want to redo this application into service to eliminate the human factor. The service will be spinning on the server 24/7. I need that she worked out every day for example at 15:00. I am self-taught and do not know much in C #. Tell me, is it possible to leave the service to get the parameters from the config.ini or all the parameters have to be stitched into the code? How to implement testing every day at 15:00, in while and checking for the current time or is there something more convenient or more correct?
1 answer
The task can be solved using one of the tasks shader for .net - such as Quartz or HangFire
An example of the implementation of the launch task every day at 15:00 using Quartz .net:
public class MyJob : Quartz.IJob { public void CreateJob() { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); IJobDetail job = JobBuilder.Create<MyJob>().Build(); ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(24) .OnEveryDay() .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(15, 0)) ) .Build(); scheduler.ScheduleJob(job, trigger); } public void Execute(IJobExecutionContext context) { // Выполнение основной задачи } } Formulation of the problem:
MyJob job = new MyJob(); job.CreateJob(); - Please in more detail. I connect to the project Quartz, compile the application. What after that, in autoloading it and it will be spinning in the background and work it out? Or how does it work? - Winteriscoming
- If we are talking about the service - then it is prescribed
Startup Type: Automatic. If a console application - then you can register it to run in Windows TaskSheduler'e, but it is probably better to consider the option of remaking this application for service. Next, in the event of theOnStartservice, we set our task:protected override void OnStart(string[] args) { MyJob job = new MyJob(); job.CreateJob(); }protected override void OnStart(string[] args) { MyJob job = new MyJob(); job.CreateJob(); }protected override void OnStart(string[] args) { MyJob job = new MyJob(); job.CreateJob(); }- Pavel Dmitrenko - Pavel thanks. Clarification, did I understand correctly that your example concerns the service? Will the service read the ini file, for this you need to register absolute paths on it? - Winteriscoming
- There is nothing about reading the file with settings in my code. To work with the settings, I recommend using App.config (in the studio, the context menu on the project -> Add New Item -> Application Configuration File):
<?xml version="1.0"?> <configuration> <appSettings> <add key="MySettings" value="Test" /> </appSettings> </configuration>and reading from this file:string s = ConfigurationManager.AppSettings["MySettings"];. In this case, you do not have to worry about the path to the configuration file. - Pavel Dmitrenko
|