public void ReadToEndTest() { WriteTestData("abcd\r\nefgh\0ijkl"); RewindStream(); var f = Reader.ReadToEnd(); f.GetCompletionEvent().Wait(); Assert.AreEqual("abcd\r\nefgh\0ijkl", f.Result); f = Reader.ReadToEnd(); f.GetCompletionEvent().Wait(); Assert.AreEqual(null, f.Result); }
public static IEnumerator<object> IssueRequest(HttpWebRequest request) { var fResponse = new Future<HttpWebResponse>(); ThreadPool.QueueUserWorkItem( (__) => { try { request.BeginGetResponse( (ar) => { try { var _ = (HttpWebResponse)request.EndGetResponse(ar); fResponse.SetResult(_, null); } catch (Exception ex) { fResponse.SetResult(null, ex); } }, null ); } catch (Exception ex_) { fResponse.SetResult(null, ex_); } } ); yield return fResponse; if (fResponse.Failed) throw new RequestFailedException(fResponse.Error); using (var response = fResponse.Result) { var fResponseStream = Future.RunInThread( () => response.GetResponseStream() ); yield return fResponseStream; Encoding encoding = AsyncTextReader.DefaultEncoding; if (!string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.GetEncoding(response.CharacterSet); string responseText; using (var stream = fResponseStream.Result) using (var adapter = new AsyncTextReader(new StreamDataAdapter(stream, false), encoding)) { var fText = adapter.ReadToEnd(); yield return fText; responseText = fText.Result; } var cookies = new Cookie[response.Cookies.Count]; response.Cookies.CopyTo(cookies, 0); yield return new Result(new Response { Body = responseText, ContentType = response.ContentType, StatusCode = response.StatusCode, StatusDescription = response.StatusDescription, Cookies = cookies }); } }
public static IEnumerator<object> RunProcessWithResult( ProcessStartInfo psi, ProcessPriorityClass? priority = null, IEnumerable<KeyValuePair<string, string>> customEnvironment = null ) { psi.UseShellExecute = false; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.WindowStyle = ProcessWindowStyle.Hidden; if (customEnvironment != null) foreach (var kvp in customEnvironment) psi.EnvironmentVariables[kvp.Key] = kvp.Value; var fProcess = StartProcess(psi); yield return fProcess; if (priority.HasValue) try { fProcess.Result.PriorityClass = priority.Value; } catch { } using (var process = fProcess.Result) using (var stdout = new AsyncTextReader( new StreamDataAdapter(process.StandardOutput.BaseStream, false), Encoding.ASCII, 1024 * 16 )) using (var stderr = new AsyncTextReader( new StreamDataAdapter(process.StandardError.BaseStream, false), Encoding.ASCII, 1024 * 16 )) try { var fStdOut = stdout.ReadToEnd(); var fStdErr = stderr.ReadToEnd(); yield return WaitForProcessExit(process); yield return fStdOut; yield return fStdErr; yield return new Result(new RunProcessResult { StdOut = fStdOut.Result, StdErr = fStdErr.Result, ExitCode = process.ExitCode }); } finally { try { if (!process.HasExited) process.Kill(); } catch { } } }