Skip to content

Commit 555fb27

Browse files
committed
feat: adding more internal protection on Process calls to prevent unexpected behavior
1 parent 00cc058 commit 555fb27

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Assets/Scripts/API/APISpikeGLX.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,31 @@
1313
public class APISpikeGLX : MonoBehaviour
1414
{
1515
[SerializeField] private TMP_InputField _helloSpikeGLXPathInput;
16+
private HashSet<Process> _processList;
1617

1718
#region Unity
1819

20+
private void Awake()
21+
{
22+
_processList = new HashSet<Process>();
23+
}
24+
1925
private void OnEnable()
2026
{
2127
GetSpikeGLXProbeInfo();
2228
}
23-
#endregion
2429

25-
#region Public
30+
private void OnDestroy()
31+
{
32+
foreach (var proc in _processList)
33+
{
34+
proc.Kill();
35+
proc.Dispose();
36+
}
37+
}
38+
#endregion
39+
40+
#region Public
2641

2742

2843
public void GetSpikeGLXProbeInfo()
@@ -180,15 +195,20 @@ private void SendAPIMessage(string msg, Action<string> callback = null)
180195
},
181196

182197
EnableRaisingEvents = true
198+
183199
};
184200

201+
_processList.Add(proc);
202+
185203
proc.OutputDataReceived += (s, d) =>
186204
{
187-
Dispatcher.Invoke(() => callback(d.Data));
205+
if (callback != null)
206+
Dispatcher.Invoke(() => callback(d.Data));
188207
};
189208

190209
proc.Exited += (s, d) =>
191210
{
211+
_processList.Remove(proc);
192212
proc.CancelOutputRead();
193213
proc.Dispose();
194214
};

0 commit comments

Comments
 (0)