Ejemplo n.º 1
0
    // cmd : string, callback : (int,string,string) -> <io|e> (), timeout : int = 0, cwd : string = "") : <io|e> () {
    public static object system(string cmd, Fun3 <int, string, string, Unit> callback, int timeout, string cwd)
    {
        ProcessStartInfo pinfo = new ProcessStartInfo();

        pinfo.UseShellExecute = true;
        if (cwd != null && cwd.Length > 0)
        {
            pinfo.WorkingDirectory = cwd;
        }
        pinfo.FileName               = "cmd.exe";
        pinfo.Verb                   = "runas";
        pinfo.Arguments              = "/c " + cmd;
        pinfo.WindowStyle            = ProcessWindowStyle.Hidden;
        pinfo.RedirectStandardError  = true;
        pinfo.RedirectStandardOutput = true;
        Process process = new Process();

        process.StartInfo           = pinfo;
        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(delegate(object sender, System.EventArgs args) {
            string stdout = process.StandardOutput.ReadToEnd();
            string stderr = process.StandardError.ReadToEnd();
            callback.Apply(process.ExitCode, stdout, stderr);
            return;
        });
        process.Start();
        if (timeout > 0)
        {
            new Timer(new TimerCallback(delegate(object state) {
                if (!process.HasExited)
                {
                    try {
                        process.Kill();
                    }
                    catch (Exception) {}
                }
            }),
                      null, timeout, Timeout.Infinite);
        }
        return(Unit.unit);
    }
Ejemplo n.º 2
0
 public static B Call <A1, A2, A3, B>(this Fun3 <A1, A2, A3, B> f, A1 x1, A2 x2, A3 x3)
 {
     return((B)f.Apply(x1, x2, x3));
 }