码迷,mamicode.com
首页 > 编程语言 > 详细

c# 定时执行python脚本

时间:2016-09-10 23:42:47      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Timers;

namespace cmdPython
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer timer = new Timer(60000);
            //timer.Interval = 60000;//执行间隔时间:60秒,单位为毫秒,一分钟执行一次判断

            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(Timer1_Elapsed);

            timer.AutoReset = true;
            timer.Start();

            Console.WriteLine("python绘制程序开始,日均值:9点20分;小时值:6点30分.执行");
            Console.ReadLine();
        }

        private static void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 得到 hour minute second  如果等于某个值就开始执行某个程序。
            int intHour = e.SignalTime.Hour;
            int intMinute = e.SignalTime.Minute;

            if (intHour == 9 && intMinute == 20)
            {
                Console.WriteLine("开始执行日均值绘制," + e.SignalTime.Date);
                doPython("python", "E:/python/drawyesteday_day.py");
            }

            if (intHour == 6 && intMinute == 30)
            {
                Console.WriteLine("开始执行小时值绘制," + e.SignalTime.Date);
                doPython("python", "E:/python/drawyesteday_hour.py");
            }
        }

        private static void doPython(string StartFileName, string StartFileArg)
        {
            Process CmdProcess = new Process();
            CmdProcess.StartInfo.FileName = StartFileName;      // 命令  
            CmdProcess.StartInfo.Arguments = StartFileArg;      // 参数  

            CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口  
            CmdProcess.StartInfo.UseShellExecute = false;
            CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入  
            CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出  
            CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  
            //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  



            CmdProcess.Start();
            CmdProcess.BeginOutputReadLine();
            CmdProcess.BeginErrorReadLine();

            // 如果打开注释,则以同步方式执行命令,此例子中用Exited事件异步执行。  
            CmdProcess.WaitForExit();
            CmdProcess.Close();
        }
    }
}

 

c# 定时执行python脚本

标签:

原文地址:http://www.cnblogs.com/tiandi/p/5860423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!