Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C#からPythonプログラムを呼び出す(非同期対応)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestUiPythonCall
{
public partial class Form1 : Form
{
StreamWriter writer;
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//テキストボックスの文字を渡す
string text = textBox1.Text;
writer.WriteLine(text);
}
private void Form1_Load(object sender, EventArgs e)
{
string fileName = @"C:\Users\ochi\Documents\temp\test.py";
Process p = new Process();
p.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
p.StartInfo.FileName = "python";
p.StartInfo.Arguments = fileName;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.BeginOutputReadLine();
writer = p.StandardInput;
writer.AutoFlush = true;
}
void DataReceived(object sender, DataReceivedEventArgs e)
{
//受け取った結果を表示する
Console.WriteLine(e.Data);
}
}
}
while True:
input_line1=input()
print(input_line1+"from python",flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment