cscの作法 その571

# 概要
cscの作法、調べてみた。
Pythonスクリプトを実行するアプリ見つけたので、やってみた。

# 参考にしたページ

https://qiita.com/kimisyo/items/0879fc9f1315e2abfd1f

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/241fd6e3-9ae2-49f4-b383-d2e66b892cd8.png)


# サンプルコード

```

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;

namespace PythonCommandExecutor
{
	public partial class Form1: Form {
		private Process currentProcess;
		private StringBuilder outStringBuilder = new StringBuilder();
		private int readCount = 0;
		private Boolean isCanceled = false;
		private Button button1,
			button2,
			button3;
		private TextBox textBox1;
		private TextBox textBox2;
		private TextBox textBox3;
		private TextBox textBox4;
		private TextBox textBox5;
		public Form1() {
			this.ClientSize = new Size(700, 500);
			this.Text = "python";
			Label la1 = new Label();
			la1.Location = new Point(30, 30);
			la1.Text = "python path";
			Label la2 = new Label();
			la2.Location = new Point(30, 60);
			la2.Text = "working directory";
			Label la3 = new Label();
			la3.Location = new Point(30, 90);
			la3.Text = "python command";
			Label la4 = new Label();
			la4.Location = new Point(30, 170);
			la4.Text = "start input";
			Label la5 = new Label();
			la5.Location = new Point(30, 300);
			la5.Size = new Size(300, 20);
			la5.Text = "standerd output and standard error";
			Controls.Add(la1);
			Controls.Add(la2);
			Controls.Add(la3);
			Controls.Add(la4);
			Controls.Add(la5);
			textBox1 = new TextBox();
			textBox1.Location = new Point(170, 30);
			textBox1.Size = new Size(300, 20);
			textBox1.Text = "c:\\python.exe";
			textBox2 = new TextBox();
			textBox2.Location = new Point(170, 60);
			textBox2.Size = new Size(300, 20);
			textBox2.Text = "c:\\work";
			textBox3 = new TextBox();
			textBox3.Location = new Point(170, 90);
			textBox3.Size = new Size(300, 20);
			textBox3.Text = "sample.py";
			textBox4 = new TextBox();
			textBox4.Location = new Point(30, 200);
			textBox4.Size = new Size(600, 100);
			textBox4.Multiline = true;
			textBox4.TabIndex = 2;
			textBox4.ScrollBars = ScrollBars.Both;
			textBox5 = new TextBox();
			textBox5.Location = new Point(30, 330);
			textBox5.Size = new Size(600, 150);
			textBox5.Multiline = true;
			textBox5.TabIndex = 2;
			textBox5.ScrollBars = ScrollBars.Both;
			Controls.Add(textBox1);
			Controls.Add(textBox2);
			Controls.Add(textBox3);
			Controls.Add(textBox4);
			Controls.Add(textBox5);
			button1 = new Button();
			button1.Location = new Point(510, 170);
			button1.Size = new Size(80, 20);
			button1.Text = "clear";
			button2 = new Button();
			button2.Location = new Point(510, 300);
			button2.Size = new Size(80, 20);
			button2.Text = "execute";
			button3 = new Button();
			button3.Location = new Point(610, 300);
			button3.Size = new Size(80, 20);
			button3.Text = "cancel";
			Controls.Add(button1);
			Controls.Add(button2);
			Controls.Add(button3);
			button1.Click += button1_Click;
			button2.Click += button2_Click;
			button3.Click += button3_Click;
		}
		public void AppendText(String data, Boolean console) {
			textBox1.AppendText(data);
			if (console)
			{
				textBox1.AppendText("\r\n");
				Console.WriteLine(data);
			}
		}
		private void button1_Click(object sender, EventArgs e) {
			button1.Enabled = false;
			button2.Enabled = true;
			isCanceled = false;
			readCount = 0;
			outStringBuilder.Clear();
			this.Invoke((MethodInvoker)(() => this.textBox1.Clear()));
			RunCommandLineAsync();
		}
		public void RunCommandLineAsync() {
			ProcessStartInfo psInfo = new ProcessStartInfo();
			psInfo.FileName = this.textBox2.Text.Trim();
			psInfo.WorkingDirectory = this.textBox3.Text.Trim();
			psInfo.Arguments = this.textBox4.Text.Trim();
			psInfo.CreateNoWindow = true;
			psInfo.UseShellExecute = false;
			psInfo.RedirectStandardInput = true;
			psInfo.RedirectStandardOutput = true;
			psInfo.RedirectStandardError = true;
			Process p = new System.Diagnostics.Process();
			p.StartInfo = psInfo;
			p.EnableRaisingEvents = true;
			p.Exited += onExited;
			p.OutputDataReceived += p_OutputDataReceived;
			p.ErrorDataReceived += p_ErrorDataReceived;
			p.Start();
			using (StreamWriter sw = p.StandardInput)
			{
				sw.Write(this.textBox5.Text.Trim());
			}
			p.BeginOutputReadLine();
			p.BeginErrorReadLine();
			currentProcess = p;
		}
		void onExited(object sender, EventArgs e) {
			int exitCode;
			if (currentProcess != null)
			{
				currentProcess.WaitForExit();
				this.Invoke((MethodInvoker)(() => AppendText(outStringBuilder.ToString(), false)));
				outStringBuilder.Clear();
				exitCode = currentProcess.ExitCode;
				currentProcess.CancelOutputRead();
				currentProcess.CancelErrorRead();
				currentProcess.Close();
				currentProcess.Dispose();
				currentProcess = null;
				this.Invoke((MethodInvoker)(() => this.button1.Enabled = true));
				this.Invoke((MethodInvoker)(() => this.button2.Enabled = false));
				if (isCanceled)
				{
					this.Invoke((MethodInvoker)(() => MessageBox.Show("処理をキャンセルしました")));
				}
				else
				{
					if (exitCode == 0)
					{
						this.Invoke((MethodInvoker)(() => MessageBox.Show("処理が完了しました")));
					}
					else
					{
						this.Invoke((MethodInvoker)(() => MessageBox.Show("エラーが発生しました")));
					}
				}
			}
		}
		void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			processMessage(sender, e);
		}
		void p_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			processMessage(sender, e);
		}
		void processMessage(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			if (e != null && e.Data != null && e.Data.Length > 0)
			{
				outStringBuilder.Append(e.Data + "\r\n");
			}
			readCount++;
			if (readCount % 5 == 0)
			{
				this.Invoke((MethodInvoker)(() => AppendText(outStringBuilder.ToString(), false)));
				outStringBuilder.Clear();
				if (readCount % 1000 == 0)
				{
					Thread.Sleep(100);
				}
			}
		}
		private void button2_Click(object sender, EventArgs e) {
			if (currentProcess != null)
			{
				try
				{
					currentProcess.Kill();
					isCanceled = true;
				}
				catch (Exception e2)
				{
					Console.WriteLine(e2);
				}
			}
		}
		private void button3_Click(object sender, EventArgs e) {
			this.textBox5.Clear();
			this.textBox1.Clear();
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。