88using System . Diagnostics ;
99using System . Diagnostics . CodeAnalysis ;
1010using System . IO ;
11+ using System . Threading ;
1112
1213namespace AdvancedSharpAdbClient
1314{
@@ -85,9 +86,9 @@ public AdbCommandLineStatus GetVersion()
8586 }
8687
8788 /// <inheritdoc/>
88- public void StartServer ( )
89+ public void StartServer ( int timeout = Timeout . Infinite )
8990 {
90- int status = RunAdbProcessInner ( "start-server" , null , null ) ;
91+ int status = RunAdbProcessInner ( "start-server" , null , null , timeout ) ;
9192 if ( status == 0 ) { return ; }
9293
9394 // Starting the adb server failed for whatever reason. This can happen if adb.exe
@@ -97,15 +98,17 @@ public void StartServer()
9798
9899 // Try again. This time, we don't call "Inner", and an exception will be thrown if the start operation fails
99100 // again. We'll let that exception bubble up the stack.
100- RunAdbProcess ( "start-server" , null , null ) ;
101+ RunAdbProcess ( "start-server" , null , null , timeout ) ;
101102 }
102103
103104 /// <inheritdoc/>
104- public virtual List < string > ExecuteAdbCommand ( string command )
105+ /// <remarks>Use this command only for <c>adb</c> commands that return immediately, such as
106+ /// <c>adb version</c>. This operation times out after 5 seconds.</remarks>
107+ public virtual List < string > ExecuteAdbCommand ( string command , int timeout = 5000 )
105108 {
106109 List < string > errorOutput = [ ] ;
107110 List < string > standardOutput = [ ] ;
108- int status = RunAdbProcessInner ( command , errorOutput , standardOutput ) ;
111+ int status = RunAdbProcessInner ( command , errorOutput , standardOutput , timeout ) ;
109112 if ( errorOutput . Count > 0 )
110113 {
111114 string error = StringExtensions . Join ( Environment . NewLine , errorOutput ! ) ;
@@ -170,12 +173,13 @@ protected virtual void EnsureIsValidAdbFile(string adbPath)
170173 /// This value can be <see langword="null"/> if you are not interested in the standard error.</param>
171174 /// <param name="standardOutput">A list in which to store the standard output. Each line is added as a new entry.
172175 /// This value can be <see langword="null"/> if you are not interested in the standard output.</param>
176+ /// <param name="timeout">The timeout in milliseconds to wait for the <c>adb</c> process to exit.</param>
173177 /// <remarks>Use this command only for <c>adb</c> commands that return immediately, such as
174- /// <c>adb version</c>. This operation times out after 5 seconds.</remarks>
178+ /// <c>adb version</c>. This operation times out after 5 seconds in default .</remarks>
175179 /// <exception cref="AdbException">The process exited with an exit code other than <c>0</c>.</exception>
176- protected void RunAdbProcess ( string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput )
180+ protected void RunAdbProcess ( string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput , int timeout = 5000 )
177181 {
178- int status = RunAdbProcessInner ( command , errorOutput , standardOutput ) ;
182+ int status = RunAdbProcessInner ( command , errorOutput , standardOutput , timeout ) ;
179183 if ( status != 0 ) { throw new AdbException ( $ "The adb process returned error code { status } when running command { command } ") ; }
180184 }
181185
@@ -188,13 +192,14 @@ protected void RunAdbProcess(string command, ICollection<string>? errorOutput, I
188192 /// This value can be <see langword="null"/> if you are not interested in the standard error.</param>
189193 /// <param name="standardOutput">A list in which to store the standard output. Each line is added as a new entry.
190194 /// This value can be <see langword="null"/> if you are not interested in the standard output.</param>
195+ /// <param name="timeout">The timeout in milliseconds to wait for the <c>adb</c> process to exit.</param>
191196 /// <returns>The return code of the <c>adb</c> process.</returns>
192197 /// <remarks>Use this command only for <c>adb</c> commands that return immediately, such as
193- /// <c>adb version</c>. This operation times out after 5 seconds.</remarks>
194- protected int RunAdbProcessInner ( string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput )
198+ /// <c>adb version</c>. This operation times out after 5 seconds in default .</remarks>
199+ protected int RunAdbProcessInner ( string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput , int timeout = 5000 )
195200 {
196201 ExceptionExtensions . ThrowIfNull ( command ) ;
197- return RunProcess ( AdbPath , command , errorOutput , standardOutput ) ;
202+ return RunProcess ( AdbPath , command , errorOutput , standardOutput , timeout ) ;
198203 }
199204
200205 /// <summary>
@@ -235,11 +240,18 @@ protected virtual void KillProcess(string processName)
235240 /// <summary>
236241 /// Runs process, invoking a specific command, and reads the standard output and standard error output.
237242 /// </summary>
243+ /// <param name="filename">The filename of the process to start.</param>
244+ /// <param name="command">The command to invoke, such as <c>version</c> or <c>start-server</c>.</param>
245+ /// <param name="errorOutput">A list in which to store the standard error output. Each line is added as a new entry.
246+ /// This value can be <see langword="null"/> if you are not interested in the standard error.</param>
247+ /// <param name="standardOutput">A list in which to store the standard output. Each line is added as a new entry.
248+ /// This value can be <see langword="null"/> if you are not interested in the standard output.</param>
249+ /// <param name="timeout">The timeout in milliseconds to wait for the process to exit.</param>
238250 /// <returns>The return code of the process.</returns>
239251#if ! HAS_PROCESS
240252 [ DoesNotReturn ]
241253#endif
242- protected virtual int RunProcess ( string filename , string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput )
254+ protected virtual int RunProcess ( string filename , string command , ICollection < string > ? errorOutput , ICollection < string > ? standardOutput , int timeout )
243255 {
244256#if HAS_PROCESS
245257 ProcessStartInfo psi = new ( filename , command )
@@ -254,7 +266,7 @@ protected virtual int RunProcess(string filename, string command, ICollection<st
254266 using Process process = Process . Start ( psi ) ?? throw new AdbException ( $ "The adb process could not be started. The process returned null when starting { filename } { command } ") ;
255267
256268 // get the return code from the process
257- if ( ! process . WaitForExit ( 5000 ) )
269+ if ( ! process . WaitForExit ( timeout ) )
258270 {
259271 process . Kill ( ) ;
260272 }
0 commit comments