@@ -95,29 +95,26 @@ private bool IsPortAvailable(int port)
9595 /// </summary>
9696 /// <param name="shellPath">Full path to trace_processor_shell.exe</param>
9797 /// <param name="tracePath">Full path to the Perfetto trace file</param>
98- public void OpenTraceProcessor ( string shellPath , string tracePath )
98+ public void OpenTraceProcessor ( string shellPath , string tracePath , DataReceivedEventHandler outputDataReceivedEventHandler , DataReceivedEventHandler errorDataReceivedEventHandler )
9999 {
100- using ( var client = new HttpClient ( ) )
101- {
102- // Make sure another instance of trace_processor_shell isn't already running
103- while ( ! IsPortAvailable ( HttpPort ) && HttpPort < PortMax )
104- {
105- HttpPort ++ ;
106- }
107-
108- ShellProcess = Process . Start ( shellPath , $ "-D --http-port { HttpPort } -i \" { tracePath } \" ") ;
109- }
110- if ( ShellProcess . HasExited )
111- {
112- throw new Exception ( "Problem starting trace_processor_shell.exe" ) ;
113- }
100+ StartTraceProcessor ( shellPath , $ "-D --http-port { HttpPort } -i \" { tracePath } \" ", outputDataReceivedEventHandler , errorDataReceivedEventHandler ) ;
114101 }
115102
116103 /// <summary>
117104 /// Initializes trace_processor_shell.exe in HTTP/RPC mode without the trace file
118105 /// </summary>
119106 /// <param name="shellPath">Full path to trace_processor_shell.exe</param>
120- public void OpenTraceProcessor ( string shellPath )
107+ public void OpenTraceProcessor ( string shellPath , DataReceivedEventHandler outputDataReceivedEventHandler , DataReceivedEventHandler errorDataReceivedEventHandler )
108+ {
109+ StartTraceProcessor ( shellPath , $ "-D --http-port { HttpPort } ", outputDataReceivedEventHandler , errorDataReceivedEventHandler ) ;
110+ }
111+
112+ /// <summary>
113+ /// Launches trace_processor_shell.exe
114+ /// </summary>
115+ /// <param name="shellPath">Full path to trace_processor_shell.exe</param>
116+ /// <param name="args"></param>
117+ private void StartTraceProcessor ( string shellPath , string args , DataReceivedEventHandler outputDataReceivedEventHandler , DataReceivedEventHandler errorDataReceivedEventHandler )
121118 {
122119 using ( var client = new HttpClient ( ) )
123120 {
@@ -126,7 +123,38 @@ public void OpenTraceProcessor(string shellPath)
126123 {
127124 HttpPort ++ ;
128125 }
129- ShellProcess = Process . Start ( shellPath , $ "-D --http-port { HttpPort } ") ;
126+
127+ var traceProcessorProcessStartInfo = new ProcessStartInfo ( )
128+ {
129+ FileName = shellPath ,
130+ Arguments = args ,
131+ RedirectStandardOutput = true ,
132+ RedirectStandardError = true ,
133+ CreateNoWindow = true ,
134+ } ;
135+
136+ ShellProcess = new Process ( ) ;
137+ ShellProcess . StartInfo = traceProcessorProcessStartInfo ;
138+
139+ if ( outputDataReceivedEventHandler != null )
140+ {
141+ ShellProcess . OutputDataReceived += outputDataReceivedEventHandler ;
142+ }
143+ if ( errorDataReceivedEventHandler != null )
144+ {
145+ ShellProcess . ErrorDataReceived += errorDataReceivedEventHandler ;
146+ }
147+
148+ ShellProcess . Start ( ) ;
149+
150+ if ( outputDataReceivedEventHandler != null )
151+ {
152+ ShellProcess . BeginOutputReadLine ( ) ;
153+ }
154+ if ( errorDataReceivedEventHandler != null )
155+ {
156+ ShellProcess . BeginErrorReadLine ( ) ;
157+ }
130158 }
131159 if ( ShellProcess . HasExited )
132160 {
@@ -317,6 +345,8 @@ public void QueryTraceForEvents(string sqlQuery, string eventKey, Action<Perfett
317345
318346 public void CloseTraceConnection ( )
319347 {
348+ ShellProcess . CancelOutputRead ( ) ;
349+ ShellProcess . CancelErrorRead ( ) ;
320350 ShellProcess ? . Kill ( ) ;
321351 }
322352 }
0 commit comments