I have below xml using which I read scheduled date and time to run an application at scheduled time.
<AvSchedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Days>
<Day Name="Monday">true</Day>
<Day Name="Tuesday">false</Day>
<Day Name="Wednesday">false</Day>
<Day Name="Thursday">false</Day>
<Day Name="Friday">false</Day>
<Day Name="Saturday">false</Day>
<Day Name="Sunday">false</Day>
</Days>
<Time Hour="12" Minutes="15" />
</AvSchedule>
Below is the code I have written to deserialize the xml:
AvSchedule avSchedule = null;
private void Deserialize()
{
FileStream fs = File.OpenRead(@"C:\Scheduler.xml");
XmlSerializer serializer = new XmlSerializer(typeof(AvSchedule));
Object obj = serializer.Deserialize(fs);
if((obj != null) && (obj is AvSchedule))
{
avSchedule = obj as AvSchedule;
}
}
Below is my implementation to run the application on service start.
protected override void OnStart(string[] args)
{
//System.Diagnostics.Debugger.Launch();
base.OnStart(args);
Deserialize();
Thread thr = new Thread( new ThreadStart(ScheduleThread));
thr.Start();
}
public void ScheduleThread()
{
//Process logic should go here
try
{
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo();
processStartInfo.Arguments = "/Task {21211-A06D-4558-B833-98E8C7F62}";
processStartInfo.FileName = "C:\\Program Files (x86)\\Logic\application.exe";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = processStartInfo;
process.Start();
}
catch (Exception e)
{
Console.WriteLine("Unable to run", e.Message);
}
}
}
///Sleep for 1 minute
Thread.Sleep(TimeSpan.FromMinutes(1));
}
}
Now I want to run the above application whenever the scheduled time is reached. I need to compare the current time and day with scheduled time and day in xml and run the application as per the scheduled time and day. Please provide implementation help for this.
Also I need make sure that service runs only when the scheduled time and day is reached ( service should be idle until the scheduled time is reached)