简体   繁体   English

使用C#的任务计划程序(要计划.bat(batch)文件)

[英]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. 我想使用与Windows Task Scheduler相同的C#创建一个Task Scheduler,以在特定时间运行我的.bat(批处理)文件。

I found this useful link ( http://www.codeproject.com/Articles/38553/TaskScheduler ) 我发现了这个有用的链接( 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, 在这种情况下,他们安排触发器,而我想安排我的.bat文件,我的意思是,当我尝试在标签文本框中提供批处理文件路径时,它只是触发了触发器,而不是运行批处理文件,因此,我修改了一下代码,现在我也可以运行我的批处理文件了

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# 注意:其桌面应用程序使用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 . 下载后,您可以检查Demo.cs ,在其中可以找到方法private void CreateSchedulerItem()和事件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 在Demo.cs页面中

In order to run your batch file or exe in TaskScheduler.cs find and replace this code 为了在TaskScheduler.cs中运行批处理文件或exe ,请查找并替换此代码

        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 . 我希望您可以更改System.Diagnostics Console.WriteLine

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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