Last active
January 19, 2017 05:41
-
-
Save ochilab/eb39e4ae9588354edb4532fd371bc0af to your computer and use it in GitHub Desktop.
C#からPythonプログラムを呼び出す(非同期対応)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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