-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLogProcForm.cs
More file actions
89 lines (79 loc) · 2.43 KB
/
MultiLogProcForm.cs
File metadata and controls
89 lines (79 loc) · 2.43 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsMultiLogProcApp1
{
public partial class MultiLogProcForm : Form
{
public MultiLogProcForm()
{
InitializeComponent();
}
private async void BtnStart_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if (b == null)
{
return;
}
b.Enabled = false;
await StartProcess(textBoxCommand.Text, textBoxArgs.Text);
b.Enabled = true;
}
private StreamWriter logFileWriter;
Process process;
private async Task StartProcess(string cmd, string cmdArgs = null)
{
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = cmd,
Arguments = cmdArgs,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
process.OutputDataReceived += (sender, args) => LogOutput(args.Data);
process.ErrorDataReceived += (sender, args) => LogOutput(args.Data);
process.Exited += (sender, args) =>
{
Invoke((Action)(() =>
{
MessageBox.Show("The process has exited", "Process Exit", MessageBoxButtons.OK);
btnStart.Enabled = true;
if (logFileWriter != null)
{
logFileWriter.Close();
}
}));
};
var logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "process.log");
logFileWriter = new StreamWriter(logFilePath, true) { AutoFlush = true };
richTextBox1.Clear();
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await Task.Run(() => process.WaitForExit());
}
private void LogOutput(string data)
{
if (!string.IsNullOrEmpty(data))
{
Console.WriteLine(data);
Debug.WriteLine(data);
logFileWriter.WriteLine(data);
Invoke((Action)(() =>
{
richTextBox1.AppendText(data);
richTextBox1.AppendText(Environment.NewLine);
}));
}
}
}
}