public int Execute() { try { Action <string> log = text => { if (!settings.Quiet) { Console.Out.WriteLine(text); } }; var wait = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.AutoReset); using (var client = new WebClientWithInfo()) { client.Headers[HttpRequestHeader.UserAgent] = settings.Http.UserAgent; client.Headers[HttpRequestHeader.Accept] = "*/*"; client.GotResponse += (object _, WebClientWithInfo.ResponseEventArgs e) => { log("Got response..."); log("Length: {0}".With(e.Response.ContentLength > -1 ? e.Response.ContentLength.ToString() : "unspecfied")); log("Saving to: '{0}'\n".With(settings.Download.OutputDocument)); }; client.DownloadProgressChanged += (object _, DownloadProgressChangedEventArgs e) => { log("progress: {0}/{1} ({2}%)".With(e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)); }; client.DownloadDataCompleted += (_, e) => wait.Set(); settings.Urls.Each(url => { client.DownloadDataAsync(new Uri(url)); // update progressbar etc wait.WaitOne(); }); } return(0); } catch (Exception ex) { if (!settings.Quiet) { Console.Error.WriteLine(ex.Message); } return(1); } }
public int Execute() { try { Action<string> log = text => { if(!settings.Quiet) Console.Out.WriteLine(text); }; var wait = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.AutoReset); using(var client = new WebClientWithInfo()) { client.Headers[HttpRequestHeader.UserAgent] = settings.Http.UserAgent; client.Headers[HttpRequestHeader.Accept] = "*/*"; client.GotResponse += (object _, WebClientWithInfo.ResponseEventArgs e) => { log("Got response..."); log("Length: {0}".With(e.Response.ContentLength > -1 ? e.Response.ContentLength.ToString() : "unspecfied")); log("Saving to: '{0}'\n".With(settings.Download.OutputDocument)); }; client.DownloadProgressChanged += (object _, DownloadProgressChangedEventArgs e) => { log("progress: {0}/{1} ({2}%)".With(e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)); }; client.DownloadDataCompleted += (_, e) => wait.Set(); settings.Urls.Each(url => { client.DownloadDataAsync(new Uri(url)); // update progressbar etc wait.WaitOne(); }); } return 0; } catch (Exception ex) { if(!settings.Quiet) Console.Error.WriteLine(ex.Message); return 1; } }