-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandPrompt.cs
113 lines (88 loc) · 3.46 KB
/
CommandPrompt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
namespace Stn.Svn
{
public class CommandPrompt
{
public static bool LastCommandPassed = true;
public static string lastCommandError = "";
public const string UNSUCCESSFUL = "ACKACKACKACK-FAILED";
public static string RunCommand(string command, bool echoOutput = true, int timeOutInSeconds = 0)
{
LastCommandPassed = true;
StringBuilder sbOut = new StringBuilder();
StringBuilder sbErr = new StringBuilder();
Process process = StartCommand(command + " & if errorlevel 1 echo " + UNSUCCESSFUL);
bool done = false;
ThreadStart threadStartStdErr = () =>
{
while (!process.HasExited)
{
string temp = "";
if (echoOutput && !process.StandardError.EndOfStream)
{
temp = process.StandardError.ReadLine();
sbErr.Append(temp + "\r\n");
Console.Write(temp + "\r\n");
}
Thread.Sleep(10);
}
};
Thread threadStdErr = new Thread(threadStartStdErr);
ThreadStart threadStartStdOut = () =>
{
while (!process.HasExited)
{
string temp = "";
if (echoOutput && !process.StandardOutput.EndOfStream)
{
temp = process.StandardOutput.ReadLine();
sbOut.Append(temp + "\r\n");
Console.Write(temp + "\r\n");
}
Thread.Sleep(10);
}
};
Thread threadStdOut = new Thread(threadStartStdOut);
threadStdErr.Start();
threadStdOut.Start();
long timeToKillProcessInTicks = DateTime.Now.Ticks + timeOutInSeconds*TimeSpan.TicksPerSecond;
while (!process.HasExited && (timeOutInSeconds == 0 || DateTime.Now.Ticks < timeToKillProcessInTicks))
{
Thread.Sleep(10);
}
if(!process.HasExited)
process.Kill();
long timeToKillThreadsInTicks = DateTime.Now.Ticks + 30 * TimeSpan.TicksPerSecond;
while((threadStdErr.IsAlive || threadStdOut.IsAlive) && DateTime.Now.Ticks < timeToKillThreadsInTicks)
Thread.Sleep(1);
if(threadStdErr.IsAlive)
threadStdErr.Abort();
if (threadStdOut.IsAlive)
threadStdOut.Abort();
string result = sbOut.ToString();
lastCommandError = sbErr.ToString();
if (result.Split(new string[] { UNSUCCESSFUL}, StringSplitOptions.RemoveEmptyEntries).Length > 2)
LastCommandPassed = false;
return sbOut.ToString();
}
public static Process StartCommand(string command)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(command);
process.StandardInput.WriteLine("exit");
process.StandardInput.Flush();
process.StandardInput.Close();
return process;
}
}
}