Question Read Time & Seconds From JSON file and Send Mail

Nikh

New member
Joined
Nov 16, 2018
Messages
1
Programming Experience
1-3
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

Public Class Service1

Protected Overrides Sub OnStart(ByVal args() As String)
Me.WriteToFile("Simple Service started at " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
Me.ScheduleService()
End Sub

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

Private Schedular As Timer

Public Sub ScheduleService()
Try
Schedular
= New Timer(New TimerCallback(AddressOf SchedularCallback))
Dim mode As String = 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)
End If
End If

If mode.ToUpper() = "INTERVAL" Then
'Get the Interval in Minutes from AppSettings.
Dim intervalMinutes As Integer = 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)
End If
End If

Dim timeSpan As TimeSpan = scheduledTime.Subtract(DateTime.Now)
Dim schedule As String = 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 As Integer = 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
As New System.ServiceProcess.ServiceController("SimpleService")
serviceController
.[Stop]()
End Using
End Try
End Sub

Private Sub SchedularCallback(e As Object)
Me.WriteToFile("Simple Service Log: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
Me.ScheduleService()
End Sub

Private Sub WriteToFile(text As String)'
 
Back
Top