Hi,

I a going to develop a new VB.net Windows service which will continuously run and read data from JSOn File and send mail.

JSON file contains 2 array with Different time [Below is the Sample JSON]. NextRuntime parameter is when the particular section of JSON file to be Execute at defined time in NEXTRUNTIME.

How to Write code to run at the specific time and run specific section from json.

'Below is the sample code of VB.NET Windows service but i am not understand how to read time from JSON file and run at specific time and seconds

{
"1":[
{
"id":"1",
"ApplicationType":"M",
"NextRunTime":"2018-11-07T01:13:05",
"DateToShow":"-1",
"To":[

"khotnikh@gmail.com"
],
"Frequency":"Daily",
"Weekly":"Monday",
}
],
"2":[
{
"id":"2",
"ApplicationType":"M",
"NextRunTime":"2018-11-07T05:00:01",
"DateToShow":"-1",
"To":[

"khotnikh@gmail.com"
],
"Frequency":"Daily",
"Weekly":"Monday",
}
]}



'Below is the sample code of Windows service but i am not understand how to read time from JSON file and run at specific time and seconds

PublicClass Service1

ProtectedOverridesSub OnStart(ByVal args()AsString)
Me.WriteToFile("Simple Service started at "+ DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
Me.ScheduleService()
EndSub

ProtectedOverridesSub OnStop()
Me.WriteToFile("Simple Service stopped at "+ DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
Me.Schedular.Dispose()
EndSub

Private Schedular As Timer

PublicSub ScheduleService()
Try
Schedular
=New Timer(New TimerCallback(AddressOf SchedularCallback))
Dim mode AsString= ConfigurationManager.AppSettings("Mode").ToUpper()
Me.WriteToFile((Convert.ToString("Simple Service Mode: ")& mode)+" {0}")

'Set the Default Time.
Dim scheduledTime As DateTime = DateTime.MinValue

If mode ="DAILY"Then
'Get the Scheduled Time from AppSettings.
scheduledTime
= DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings("ScheduledTime"))
If DateTime.Now > scheduledTime Then
'If Scheduled Time is passed set Schedule for the next day.
scheduledTime
= scheduledTime.AddDays(1)
EndIf
EndIf

If mode.ToUpper()="INTERVAL"Then
'Get the Interval in Minutes from AppSettings.
Dim intervalMinutes AsInteger= Convert.ToInt32(ConfigurationManager.AppSettings("IntervalMinutes"))

'Set the Scheduled Time by adding the Interval to Current Time.
scheduledTime
= DateTime.Now.AddMinutes(intervalMinutes)
If DateTime.Now > scheduledTime Then
'If Scheduled Time is passed set Schedule for the next Interval.
scheduledTime
= scheduledTime.AddMinutes(intervalMinutes)
EndIf
EndIf

Dim timeSpan As TimeSpan = scheduledTime.Subtract(DateTime.Now)
Dim schedule AsString=String.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds)

Me.WriteToFile((Convert.ToString("Simple Service scheduled to run after: ")& schedule)+" {0}")

'Get the difference in Minutes between the Scheduled and Current Time.
Dim dueTime AsInteger= Convert.ToInt32(timeSpan.TotalMilliseconds)

'Change the Timer's Due Time.
Schedular
.Change(dueTime, Timeout.Infinite)
Catch ex As Exception
WriteToFile
("Simple Service Error on: {0} "+ ex.Message + ex.StackTrace)

'Stop the Windows Service.
Using serviceController
AsNew System.ServiceProcess.ServiceController("SimpleService")
serviceController
.[Stop]()
End Using
EndTry
EndSub

PrivateSub SchedularCallback(e AsObject)
Me.WriteToFile("Simple Service Log: "+ DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
Me.ScheduleService()
EndSub

PrivateSub WriteToFile(text AsString)'