Mono:如何以阻塞方式执行 shell 脚本?

Mono: How to execute a shell script in a blocking manner?

我正在尝试从 Mono 调用 bash 脚本。因为脚本依次调用了"pico2wave"(Android的Pico TTS语音的Linux实现),调用脚本必然会阻塞Mono代码的执行。

我当然可以将脚本修改为 touch/rm 作为锁的文件,但是有没有办法在 Mono 代码中完成这个任务?

感谢任何建议,

卡斯滕

您可以使用 Process.WaitForExit 等待(阻止)直到您的脚本执行完毕。

Process.WaitForExit Method

Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. To avoid blocking the current thread, use the Exited event.

示例 shell 脚本休眠 5 秒然后退出,名为 shell-block.sh:

#!/bin/bash
sleep 5
exit 0

示例 C#:

using System;
using System.Diagnostics;

namespace consoleblocking
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Non-blocking");
            Process.Start ("./shell-block.sh");

            Console.WriteLine ("Blocking");
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "./shell-block.sh";
            startInfo.Arguments = "";
            using (var myProcess = Process.Start(startInfo))
            {
                myProcess.WaitForExit();
            }
        }
    }
}