• c# 定时执行python脚本


    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();
            }
        }
    }
  • 相关阅读:
    一、maven的安装及配置
    Mybatis分页助手PageHelper
    $('#itemAddForm').form('reset');重置表单是报错?
    个人遇到的几种Date类型处理方式
    eclipse中tomcat的add and remove找不到项目
    print,printf,println的区别,以及 , , 的区别
    linux权限问题,chmod命令
    前后端分离怎么部署
    linux安装jdk
    springboot jar包方式部署
  • 原文地址:https://www.cnblogs.com/kongxiaoshuang/p/6934066.html
Copyright © 2020-2023  润新知