C#运行Python process.BeginOutputReadLine()挂起直到退出

时间:2015-10-25 11:04:07

标签: c# python process

我正在尝试使用process.BeginOutputReadLine()

从c#执行Python

我的问题是python的接收结果只显示python prog完成后并没有真正执行readline 这是我的代码

public void ExecuteProcess()
    {


        using (var process = new Process())
        {
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.FileName = "python.exe";
            process.StartInfo.Arguments = string.Format("{0} ", @"C:\FB\mytestpython.py");

            process.OutputDataReceived += (sender, args) =>
            {
                if (args.Data != null)
                {
                    //write data 

                    UpdatePythonLog(args.Data);

                }
            };

            process.Start();

            process.BeginOutputReadLine();

        }

        return ;
    }

这是我的Python代码:

import time

if __name__=='__main__':

    print "test1"  
    time.sleep(5) 
    print "test2"  
    time.sleep(5) 
    print "test3"  
    time.sleep(5) 
    print 'finish'  

0 个答案:

没有答案