0

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)

dsolimano
  • 8,870
  • 3
  • 48
  • 63
user004
  • 3
  • 1
  • 3

1 Answers1

1

I know i'm not replying to your question directly but... don't reinvent the wheel!

Use Quartz.net

Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

It is very robust and reliable and you can do any type of scheduling.

It handles very well different threads instead of running processes as you are doing.

It has xml configuration and it is full features.

I've used it in many project and it works very well.

---UPDATE AFTER COMMENT---------

Use a Timer

code below is just an example you can adapt to your needs

public class ScheduleThread 
{
    private readonly TimeSpan _timeSpan;
    private readonly Timer _timer;
    public bool IsRunning { get; protected set; }   
    public ScheduleThread()
    {
        IsRunning = false;
                    //set the timespan according your config            
        _timeSpan = new TimeSpan();
    }
    public void Start()
    {
        if (IsRunning == false)
        {
            IsRunning = true;
            StartTimer();
        }
    }
    private void StartTimer()
    {
                    _timer = new Timer(CallBack);
        _timer.Change(_timeSpan, TimeSpan.Zero);
    }
    public void Stop()
    {
        if (IsRunning)
        {
            _timer.Dispose();
            IsRunning = false;
        }
    }
    private void CallBack(object obj)
    {
        //if process is not running
        //start process
    }       
}
Community
  • 1
  • 1
giammin
  • 18,620
  • 8
  • 71
  • 89