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();
//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 == && intMinute == )
{
Console.WriteLine("开始执行日均值绘制," + e.SignalTime.Date);
doPython("python", "E:/python/drawyesteday_day.py");
} if (intHour == && intMinute == )
{
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();
}
}
}