Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active January 19, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/eb39e4ae9588354edb4532fd371bc0af to your computer and use it in GitHub Desktop.
Save ochilab/eb39e4ae9588354edb4532fd371bc0af to your computer and use it in GitHub Desktop.
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