public static ExecScriptWithLog ExecScriptWithLog() { return((Statement statement, String tailPars, ProgressBar bar, TextBox txtBox, String ServerName, String serverUser, String serverPass) => { bar.Value = 0; BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += (o, workerE) => { String feedback = ""; worker.ReportProgress(50); try { feedback = SSHCommander.RunCommandWithFeedback(ServerName, serverUser, serverPass, statement.ScriptToLaunch + tailPars.ToSafeString()); worker.ReportProgress(10); workerE.Result = feedback; worker.ReportProgress(10); } catch (Exception ex) { } worker.ReportProgress(30); }; worker.ProgressChanged += (o, e) => { bar.Value += e.ProgressPercentage; }; worker.RunWorkerCompleted += (o, e) => { txtBox.Text = e.Result.ToSafeString(); Task.Factory.StartNew(() => { WriteFSLog()(e.Result.ToSafeString(), Path.Combine(Environment.CurrentDirectory, statement.ScriptToLaunch + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString() + ".log")); }); }; worker.RunWorkerAsync(); }); }
public static StartProcess StartProcess() { return((Statement statement, String tailPars, TextBox txtBox, ProgressBar bar) => { Action cancel = () => { txtBox.Text = ""; }; txtBox.Dispatcher.Invoke(cancel); if (statement.ScriptToLaunch.IndexOf(".exe") == -1 && statement.ScriptToLaunch.IndexOf(".jar") == -1) { ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + statement.ScriptToLaunch + " " + tailPars); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; Process proc = new Process(); proc.StartInfo = procStartInfo; proc.Start(); string result = proc.StandardOutput.ReadToEnd(); txtBox.Text = result; } else { // se è un exe lo lancio Process process = new Process(); var res = new StringBuilder(); var resError = new StringBuilder(); process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = statement.ScriptToLaunch; process.StartInfo.Arguments = statement.Parameters + " " + tailPars; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; try { if (process.Start()) { process.OutputDataReceived += new DataReceivedEventHandler ( delegate(object sender, DataReceivedEventArgs e) { Action pr = () => { txtBox.Text += e.Data + "\n"; }; txtBox.Dispatcher.Invoke(pr); res.AppendLine(e.Data); } ); process.ErrorDataReceived += new DataReceivedEventHandler ( (ob, ev) => { resError.AppendLine("Rilevato un errore: " + ev.Data); } ); process.Exited += new EventHandler ( (ob, ev) => { txtBox.Text += "Processo terminato"; Task.Factory.StartNew(() => { WriteFSLog()(res.ToString() + " \n\nErrori:\n " + resError.ToString(), Path.Combine(Environment.CurrentDirectory, (statement.ScriptToLaunch + statement.Parameters).Replace(" ", "_").Replace(".", "_") + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString() + ".log")); }); } ); process.BeginOutputReadLine(); process.BeginErrorReadLine(); } } catch (Exception ex) { txtBox.Text = ex.Message; } } }); }