forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessOutputStreamReader.cs
More file actions
72 lines (65 loc) · 2.15 KB
/
Copy pathProcessOutputStreamReader.cs
File metadata and controls
72 lines (65 loc) · 2.15 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace UnityEditor.Utils
{
internal class ProcessOutputStreamReader
{
private readonly Func<bool> hostProcessExited;
private readonly StreamReader stream;
internal List<string> lines;
private Thread thread;
internal ProcessOutputStreamReader(Process p, StreamReader stream) : this(() => p.HasExited, stream)
{
}
internal ProcessOutputStreamReader(Func<bool> hostProcessExited, StreamReader stream)
{
this.hostProcessExited = hostProcessExited;
this.stream = stream;
lines = new List<string>();
thread = new Thread(ThreadFunc);
thread.Start();
}
private void ThreadFunc()
{
try
{
while (true)
{
if (stream.BaseStream == null) return;
string line = stream.ReadLine();
if (line == null)
return;
lock (lines)
{
lines.Add(line);
}
}
}
catch (ObjectDisposedException)
{
// We have had this throw in a run on Katana in what appears to be a case of a very short running
// process exiting between the check to hostProcessExited() and the call to stream.ReadLine();
// So catch this case to avoid this from happening again.
lock (lines)
{
lines.Add("Could not read output because an ObjectDisposedException was thrown.");
}
}
}
internal string[] GetOutput()
{
if (hostProcessExited())
thread.Join();
lock (lines)
{
return lines.ToArray();
}
}
}
}