简体   繁体   中英

Process stops immediately after start

Hope you can help me. I'm coding on a Raspberry Pi with MonoDevelop.

I want to execute a python script with C# and read from it.

class Program 
{
    public static void Main(string[] args)
    {
        Process p = new Process();
        p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
        p.StartInfo.FileName = "sudo";
        p.StartInfo.Arguments = "python gpio.py";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardOutput = true;

        p.Start();
        p.BeginOutputReadLine();
        p.WaitForExit();
    }

    private static void OutputHandler(Object sender, DataReceivedEventArgs args) 
    {
        Console.WriteLine(args.Data);
    }
}

While Debugging i can see that the Process has exited Click for image

But in the TaskManager i can see, that the process is still running. Also the script controls the gpio pins. And the script controlls the pins (Led on/off), even if the "Process has exited" . But I dont get anything from redirectOutput.

Why does the Process immediately quits after starting (the script has a while true. it shouldn't stop)? Is this the right way to execute a script?
If I execute the Python script from the terminal, it works fine. It shouldn't be an error with the script. If I start a process eg FileName "libreoffice", it works too.

The script is located in the project folder in "/bin/Debug/" (the folder) Permissions for execution are set for anyone.

Thanks,
Greetings

As @Gusman said, the problem was the sudo. And as recommended i am using now the DLL to access the GPIO Pins. Even if the Raspberry Pi is not fully supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM