How can I do jobs in a specific millisecond? I want to do jobs at 02:00:00.200.200, 02:00:00.400, 02:00:00.600 and ... . Is this possible in C#? Jobs may take longer. Can they be done on time? For example, run in the background?
Asked
Active
Viewed 471 times
-1
-
https://www.quartz-scheduler.net/ – stuartd Sep 28 '20 at 16:30
-
It is a DateTime object. new DateTime(year, month, day, hour, minute, second, millisecond) – jdweng Sep 28 '20 at 16:32
-
Can you show us what you already have or plan on doing? When dealing with stuff in this nature, people will write how they want to accomplish the task. It sounds like you want to run things in the background, but the period is so short that a task might not make sense. Typically when using a background task scheduler, the duration is expanded into minutes, hours, and days. Almost sounds like you need a service. – Kevin B Burns Sep 28 '20 at 19:15
-
I want to do this @KevinBBurns – phpDeveloper Sep 28 '20 at 20:37
-
I used quartz but it does not have the withIntervalInMilliseconds method @jdweng – phpDeveloper Sep 28 '20 at 20:40
-
There is no different between Net and Quartz how a Datetime is stored as a variable. Both use Ticks which are 100ns. So to add milliseconds you just add 10,000 ticks per 1 millisecond. – jdweng Sep 28 '20 at 21:23
-
Thank you. Can you send a sample code? I'm an amateur in C# @jdweng – phpDeveloper Sep 29 '20 at 10:12
-
I do not have Quartz installed on my machine. – jdweng Sep 29 '20 at 10:23
2 Answers
0
You can try to improve timer accuracy with winmm.dll timeBeginPeriod function call
look at this link how to set timer resolution from C# to 1 ms? for more information

volody
- 6,946
- 3
- 42
- 54
0
@jdweng I use the following codes, but the job is not done in 200, 400, 600, etc. milliseconds. The schedule also starts at 2:0:0.500
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(DateBuilder.DateOf(2, 0, 0))
.WithSimpleSchedule(x => x
.WithInterval(TimeSpan.FromMilliseconds(200))
.WithRepeatCount(5))
.Build();
await scheduler.ScheduleJob(job, trigger);
await Task.Delay(TimeSpan.FromSeconds(60));
await scheduler.Shutdown();

phpDeveloper
- 21
- 1
- 2
-
Are you doing any kind of logging? How do you know it is not firing the job? The good looks fine, and should in theory run without problems. – Kevin B Burns Sep 29 '20 at 18:30
-
Yes. Jobs do not run at my preferred times. For example, I want to be done at 2:0:0.200 and 2:0:0.400, etc., but all jobs are done at 2: 0: 0,900 (for example) @KevinBBurns – phpDeveloper Sep 30 '20 at 11:36