locked
Task scheduler in C#(windows)

    Question

  • hello all,

    i am trying to make a task scheduler using C#.net (a desktop application). I have an example to help in doing this job which registers an exe file compliled using C# in visual studio. According to this example, this service after registering runs and looks which job is scheduled and at what time. When found, a particuler job(programe) runs automatically.

    I have done accoding to this example but i did not achieve my goal! I have a poor idea of such assignments! I request you to please help me!

    Here is the code that I am using:

    (A component file)

    using System;

    using System.ComponentModel;

    using System.Collections;

    using System.Diagnostics;

    using System.ServiceProcess;

    using System.Xml;

    using System.Timers;

    using System.Threading;

    using System.Configuration;

    using System.IO;

    using System.Data;

    namespace TaskScheduler

    {

    public class AppScheduler : System.ServiceProcess.ServiceBase

    {

    string configPath;

    System.Timers.Timer timer1=new System.Timers.Timer();

    DataSet ds=new DataSet();

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

    /// <summary>

    /// Class that launches applications on demand.

    /// </summary>

    class AppLauncher

    {

    string app2Launch;

    public AppLauncher(string path)

    {

    app2Launch=path;

    }

    public void runApp()

    {

    ProcessStartInfo pInfo=new ProcessStartInfo(app2Launch);

    pInfo.WindowStyle=ProcessWindowStyle.Normal;

    Process p=Process.Start(pInfo);

    System.IO.File.Create("C:\\schedule.txt");

    }

    }

    void timeElapsed(object sender, ElapsedEventArgs args)

    {

    DateTime currTime=DateTime.Now;

    foreach(DataRow dRow in ds.Tables["task"].Rows)

    {

    DateTime runTime=Convert.ToDateTime(dRow["time"]);

    string formatString="MM/dd/yyyy HH:mm";

    if(runTime.ToString(formatString)==currTime.ToString(formatString))

    {

    System.IO.File.Create("timer.txt");

    string exePath=dRow["exePath"].ToString();

    AppLauncher launcher=new AppLauncher(exePath);

    new Thread(new ThreadStart(launcher.runApp)).Start();

    // Update the next run time

    string strInterval=dRow["repeat"].ToString().ToUpper();

    // switch(strInterval)

    //

    // {

    //

    // case "D":

    //

    // runTime=runTime.AddSeconds(1);

    //

    // break;

    //

    // case "W":

    //

    // runTime=runTime.AddDays(7);

    //

    // break;

    //

    // case "M":

    //

    // runTime=runTime.AddMonths(1);

    //

    // break;

    //

    // }

    dRow["time"]=runTime.ToString(formatString);

    ds.AcceptChanges();

    StreamWriter sWrite=new StreamWriter(configPath);

    XmlTextWriter xWrite=new XmlTextWriter(sWrite);

    ds.WriteXml(xWrite, XmlWriteMode.WriteSchema);

    xWrite.Close();

    }

    }

    }

    public AppScheduler()

    {

    // This call is required by the Windows.Forms Component Designer.

    InitializeComponent();

    // TODO: Add any initialization after the InitComponent call

    }

    // The main entry point for the process

    static void Main()

    {

    System.ServiceProcess.ServiceBase[] ServicesToRun;

    System.IO.File.Create("C:\\mainm.txt");

    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new AppScheduler() };

    System.ServiceProcess.ServiceBase.Run(ServicesToRun);

    }

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    //

    // AppScheduler

    //

    this.CanPauseAndContinue = true;

    this.ServiceName = "Application Scheduler";

    System.IO.File.Create("Initallize.txt");

    }

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

    if( disposing )

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    base.Dispose( disposing );

    }

    /// <summary>

    /// Set things in motion so your service can do its work.

    /// </summary>

    protected override void OnStart(string[] args)

    {

    // TODO: Add code here to start your service.

    configPath=ConfigurationSettings.AppSettings["configpath"];

    try

    {

    XmlTextReader xRead=new XmlTextReader(configPath);

    XmlValidatingReader xvRead=new XmlValidatingReader(xRead);

    xvRead.ValidationType=ValidationType.DTD;

    ds.ReadXml(xvRead);

    xvRead.Close();

    xRead.Close();

    }

    catch(Exception)

    {

    Console.WriteLine("Exception thrown");

    ServiceController srvcController=new ServiceController(ServiceName);

    srvcController.Stop();

    }

    timer1.Interval=3000;

    timer1.Elapsed+=new ElapsedEventHandler(timeElapsed);

    timer1.Start();

    }

    /// <summary>

    /// Stop this service.

    /// </summary>

    protected override void OnStop()

    {

    // TODO: Add code here to perform any tear-down necessary to stop your service.

    }

    }

    }

    an xml file (AppScheculer.xml)containing the job(programs) to run:

    <?xml version="1.0" encoding="utf-8" ?>

    <!DOCTYPE appSchedule[

    <!ELEMENT appSchedule (task*)>

    <!ELEMENT task EMPTY>

    <!ATTLIST task name CDATA #REQUIRED>

    <!ATTLIST task exePath CDATA #REQUIRED>

    <!ATTLIST task time CDATA #REQUIRED>

    <!ATTLIST task repeat (D) #REQUIRED>

    ]>

    <appSchedule>

    <task name="Notepad" exePath="%SystemRoot%\system32\notepad.exe" time="08/09/2006 12:58" repeat="D"/>

    <task name="Calculator" exePath="%SystemRoot%\System32\calc.exe" time="08/08/2006 18:47" repeat="D"/>

    </appSchedule>

    and the app.config file which tells the location of this above xml file(I keep this above xml file at c:\)

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    <appSettings>

    <add key="configpath" value="C:\AppScheculer.xml"/>

    </appSettings>

    </configuration>

    when all this is done, i build my project(this is windows application) and install the exe file in Services(located in Control Panel > Administrative tools), but nothing happens. I don't know where is wrong or what is wrong!!

    Please help me!!

    Wednesday, August 09, 2006 11:46 AM

All replies

  • Go through this link, this might help you.....

    http://www.c-sharpcorner.com/UploadFile/ajifocus/AppScheduler05262006074807AM/AppScheduler.aspx?ArticleID=b52f76da-943f-4807-9675-869f135ef2cd


    Warm regards,
    Sabah Tahir
    Monday, August 28, 2006 9:25 AM
  • Dear Sabah,

    Is there a way to write the same program for PPC 2003 plateform?

    Regards,
    Tuesday, January 19, 2010 7:17 AM
  • you can schedule using window scheduler by giving the path of the app exe.

     

    i you really want to create new scheduler and cannot use window scheduler refer http://www.codeproject.com/KB/shell/scheduler.aspx

    Friday, April 08, 2011 8:24 AM