Почему функция Python print () ничего не возвращает при вызове из C #? - PullRequest
0 голосов
/ 04 февраля 2019

Я пытаюсь запустить скрипт Python из C # - он отлично работает, когда я запускаю его через cmd.Каждый раз, когда я пытаюсь запустить его из C #, я не получаю вывод print ().Когда я добавляю больше кода в скрипт Python, все работает нормально, т.е. копирование изображений, ... не работает только функция print ().

Программный код Python "DoSomething.py":

import time                  

#####     MAIN     #####
print("Starte Programm")



if __name__ == "__main__":
    print("Programm really started...")


    try:
        while True:
            time.sleep(0.001)
    except KeyboardInterrupt:
            print("Interrupted")

    print("ende")

Результат работает в Windows CMD

Код программы C #:

    UserControl1 crtl = new UserControl1(); //Create UserControl to show Output in WPF
    crtl.DataContext = this;
    MyStack.Children.Add(crtl);
    //Running cmd as Administrator
    var pass = new SecureString();
    pass.AppendChar('T');
    pass.AppendChar('e');
    pass.AppendChar('s');
    pass.AppendChar('t');

    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.WorkingDirectory = @"C:\users\admincs\";
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.Verb = "runas";

    p.StartInfo.UserName = "admincs";
    p.StartInfo.Password = pass;
    p.StartInfo.Domain = "test";
    //activate virtual environment for python 
    p.StartInfo.Arguments = @"/k ""activate deeplearning""";

    p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
    {
        //    Prepend line numbers to each line of the output.
        if (!String.IsNullOrEmpty(e.Data))
        {
            lineCount++;
            output.Append("\n[" + lineCount + "]: " + e.Data);
            _consoleOutputText = output.ToString();
        }
    }); 


    p.Start(); 

    //read the command line output 
    using (StreamWriter sw = p.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine(@"python DoSomething.py");
        }
    }

    // This raises OutputDataReceived events for each line of output.
    p.BeginOutputReadLine();

    // Write the redirected output to this application's window.
    _consoleOutputText = output.ToString();

    p.WaitForExit();
    p.Close();

CMD ничего не возвращает - нет результата print ()

Стек WPF не возвращает результат печати ()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...