简体   繁体   中英

Task Scheduler using C# (To Schedule .bat(batch) file)

I want to create one Task Scheduler using C# same as Windows Task Scheduler, to run my .bat (batch) file on particular time.

I found this useful link ( http://www.codeproject.com/Articles/38553/TaskScheduler )

in this they schedule trigger, and i want to Schedule my .bat file I mean while i am trying to give my batch file path in tags textbox, its just fired trigger, not run my batch file so, i modify that code little bit, and now I am able to run my batch file also,

but, when i close my application triggering also stop, so, is there any way i can triggering or run my batch file even if i close my application liks window task scheduler???

kindly Help me .

Note: its desktop application using C#

So, what's the problem? After downloading you can check the Demo.cs , where you can find the method private void CreateSchedulerItem() and the event triggerItem_OnTrigger . You can change this event to run the batch file that you need.

you can place the path of your batch file in tags textbox .Checkbox the Active in one time only box , set the time and at that time the trigger is fired.

Note: It justs fire's the trigger.What you are loking is to run the batch file.In that case you need to modify the code. you can start from

private void buttonCreateTrigger_Click(object sender, EventArgs e)
    {
        CreateSchedulerItem();
    }

in Demo.cs page

In order to run your batch file or exe in TaskScheduler.cs find and replace this code

        void _triggerTimer_Tick(object sender, EventArgs e)
    {
        _triggerTimer.Stop();
        foreach (TriggerItem item in TriggerItems)
            if (item.Enabled)
                while (item.TriggerTime <= DateTime.Now)
                    item.RunCheck(DateTime.Now);
                    System.Diagnostics.Process.Start("Your Path");
        _triggerTimer.Start();
    }

Now you can save this path in some knid of common class and make it access to both.

Something like this. I hope that you can change Console.WriteLine on System.Diagnostics .

static void Main(string[] args) {
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
    Timer timer = new Timer(PrintHello, autoResetEvent, 0, 5000);
    autoResetEvent.WaitOne();
}

private static void PrintHello(Object state) {
    Console.WriteLine("Hello");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM