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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM