0

Following this problem I couldn't find any solution to,

I'm now trying to run a python script from C#, with an infinite loop inside, printing a result at each iteration. And I want to get each result in C#.

So this is my python script :

#!/usr/bin/env python3

import time

while(True) :
    print(1)
    time.sleep(0.5)

And here is my C# program :

static void Main()
        {
            using (Process myProcess = new Process())
            {
                var errors = "";
                var results = "";

                myProcess.StartInfo.FileName = "python";
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.Arguments = "../../test.py";
                myProcess.StartInfo.RedirectStandardOutput = true;


                myProcess.Start();
                StreamReader outputStream = myProcess.StandardOutput;

                while(true) 
                {
                          results = outputStream.ReadToEnd();

                          Console.WriteLine("Results:");
                          Console.WriteLine(results);
                          outputStream.DiscardBufferedData();
                }
            }
        }

And obviously, nothing gets printed to the console.

But I have no clue how I'm supposed to change my code to get back and use the "1"s.

It sounds like it should be quite easy to do, yet I've been stuck trying to make it work for some hours now. I'm currently thinking about sending my python script's results to a text file, and reading back the text file in C#. But I'd rather not have to do this, cause it looks ugly and will cause (avoidable, yet annoying) conflicts.

So, is there something I can do ?

Thanks for your help,

Astrosias.

Astrosias
  • 23
  • 4
  • Common issue I've been dealing with for a long time. When transmitting/Receiving data you need to be able to determine where the end of each message occurs. So you use one of three methods 1) ASCII : Terminate each message with a character not in the message like a "\n" or EOT. 2) Binary, ASCII : Proceed each message with a byte count 3) ASCII, Binary : Fixed Length Message – jdweng Jun 10 '20 at 16:06
  • Since you have control of the python script, [Named Pipes](https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipes)? – Idle_Mind Jun 10 '20 at 16:09
  • Well, I've been trying to use pipes this morning, and nothing good came out of it (mostly because I don't know much about them). – Astrosias Jun 11 '20 at 12:52
  • And concerning the termination of my messages, I didn't see any change (but maybe I edited the code in a wrong way). Yet, even if python sends rightly typed messages, this code still won't get anything back, isn't it ? – Astrosias Jun 11 '20 at 12:55

0 Answers0