public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try{ ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c"; #endif if (environmentVars != null) { foreach (string var in environmentVars) { start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.Arguments += (" \"" + cmd + " \""); start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); _queue.Add(delegate() { req.Log(0, line); }); }while(true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; _queue.Add(delegate() { req.Log(1, error); }); } p.Close(); if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } }catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } }); return(req); }
public static ShellRequest ProcessFileCommand(string fileName, string parameters, string workDirectory = null) { ShellRequest req = new ShellRequest(); Process p = null; try { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = fileName; start.Arguments = parameters; start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; if (workDirectory != null) { start.WorkingDirectory = workDirectory; } if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); _queue.Add(delegate() { req.Log(0, line); }); } while (true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; _queue.Add(delegate() { req.Log(1, error); }); } p.Close(); if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } return(req); }
public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.Interlocked.Increment(ref numberOfTasks); //var threadFinish = new System.Threading.ManualResetEvent(false); //threadFinishEvents.Add(threadFinish); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try { ProcessStartInfo start = CreateProcessStartInfo(cmd, workDirectory, environmentVars); p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); lock (_queue) { _queue.Add(delegate() { req.Log(0, line); }); } } while (true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; lock (_queue) { _queue.Add(delegate() { req.Log(1, error); }); } } p.Close(); lock (_queue) { if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); //直接在线程中触发事件,这个callback中不能用序列化 req.TriggerEdge(); } } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } finally { if (System.Threading.Interlocked.Decrement(ref numberOfTasks) == 0) { threadSignal.Set(); } } }); return(req); }
public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try { ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; start.Arguments += (" \"" + cmd + " \""); #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c" + cmd; #endif if (environmentVars != null) { foreach (string var in environmentVars) { start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; #if UNITY_EDITOR_OSX start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; #else if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; var ci = System.Globalization.CultureInfo.GetCultureInfo(GetSystemDefaultLCID()); start.StandardOutputEncoding = Encoding.GetEncoding(ci.TextInfo.OEMCodePage); start.StandardErrorEncoding = Encoding.GetEncoding(ci.TextInfo.OEMCodePage); } #endif p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); p.Close(); if (!string.IsNullOrEmpty(output)) { _queue.Add(delegate() { req.Log(0, EncodingConvert(output, start.StandardErrorEncoding, Encoding.UTF8)); }); } if (!string.IsNullOrEmpty(error)) { _queue.Add(delegate() { req.Log(1, EncodingConvert(error, start.StandardErrorEncoding, Encoding.UTF8)); }); } if (!string.IsNullOrEmpty(error)) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } }); return(req); }
public static ShellRequest ProcessCommand(string cmd,string workDirectory,List<string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try{ ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c"; #endif if(environmentVars != null){ foreach(string var in environmentVars){ start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.Arguments += (" \"" + cmd + " \""); start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; if(start.UseShellExecute){ start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else{ start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do{ string line = p.StandardOutput.ReadLine(); if(line == null){ break; } line = line.Replace("\\","/"); _queue.Add(delegate() { req.Log(0,line); }); }while(true); while(true){ string error = p.StandardError.ReadLine(); if(string.IsNullOrEmpty(error)){ break; } hasError = true; _queue.Add(delegate() { req.Log(1,error); }); } p.Close(); if(hasError){ _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } }catch(System.Exception e){ UnityEngine.Debug.LogException(e); if(p != null){ p.Close(); } } }); return req; }