1

I have a windows client for a data monitoring application written in C#. And I have a python script compiled with Pyinstaller that keeps sending data to the client every few seconds.

When I manually run the python application ( by double-clicking it ), it runs perfectly ( keeps sending the data continuously ), however, when I start the application in C#, it stops sending the data after 2-3 minutes.

I read about ironpython but thought of doing it the old way as the script had a few dependencies.

This is the code I'm using to spawn the python process:

Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "relative-path-to-exe";
process.Start();

I've even tried to set the process priority to High/Realtime using the following code, but it didn't help:

process.PriorityClass = ProcessPriorityClass.RealTime;

Any idea what might be causing this issue ?

mrid
  • 5,782
  • 5
  • 28
  • 71
  • Do you have `process.WaitForExit();` somewhere? Or have you subscribed to the `Exited` event? – Jimi May 09 '19 at 07:06
  • @Jimi no I haven't used `WaitForExit()` anywhere. The process keeps running (it is visible in the Task Manager processes ), it just stops working – mrid May 09 '19 at 07:16
  • Then maybe set `process.EnableRaisingEvents = true`, subscribe to the `Exited` event (so you'll have the asynchronous version), and `process.StartInfo.RedirectStandardError = true;`. Then you need a handler for `process.OutputDataReceived`... Sample code [here](https://stackoverflow.com/a/51682585/7444103). – Jimi May 09 '19 at 07:22
  • @Jimi I tried your code but it doesn't give me any continuous output (just the last `print()` output when i terminate the script). hovever, it does work a lot longer (still testing it out at the moment) – mrid May 10 '19 at 12:05
  • @Jimi can you please explain why this is working better than the simple code i was earlier using :/ – mrid May 10 '19 at 12:06
  • The event are not trigger? no even once? but it works now. that's strange. I had a no repro on my side with a "do nothing" py script. – xdtTransform May 10 '19 at 12:15
  • If you don't have neither `WaitForExit` nor subscribed to the `Exited` event, your Process object won't hold to the process it started (eventually, will go out of scope) and will be garbage collected. You need to either use the synchronous *Wait State* or the asynchronous (event-driven) one. *it doesn't give me any continuous output*: note that the post I linked is built to send and receive commands through the `cmd.exe` console. It may not be perfectly suited to receive the output of the program you're using. Which may buffer the output and flush it when it deems it necessary. – Jimi May 10 '19 at 13:42
  • You can use that code and fine-tune it for your purpose. I doubt it needs the `cmd` console to work, so you may want to test it with different configurations, always redirecting `stdout` and `stderr`. The StandardInput is not required (unless you need to interact with it while it runs; if it's possible, I have not tested it) – Jimi May 10 '19 at 13:42

0 Answers0