public static void DownloadText(string address, Stopwatch?watch, DownloadTextResultHandler callback, bool ignoreCache, out bool isFound) { string FileName = LocalFileName(address); string FilePath = Path.GetDirectoryName(FileName); if (!ignoreCache && File.Exists(FileName)) { using (FileStream Stream = new FileStream(FileName, FileMode.Open, FileAccess.Read)) { using (StreamReader Reader = new StreamReader(Stream)) { string Content = Reader.ReadToEnd(); isFound = !string.IsNullOrEmpty(Content); callback(isFound, Content); } } } else { if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } Task <Tuple <bool, string?> > DownloadTask = new Task <Tuple <bool, string?> >(() => { return(ExecuteDownloadText(address, watch)); }); DownloadTask.Start(); OnCheckDownload(DownloadTask, address, callback, out isFound); } }
/// <summary> /// Downloads a page of text from the Internet. /// </summary> /// <param name="address">The page address.</param> /// <param name="callback">The download handler.</param> public static void DownloadText(string address, DownloadTextResultHandler callback) { Task <DownloadResult> DownloadTask = new Task <DownloadResult>(() => { return(ExecuteDownloadText(address)); }); DownloadTask.Start(); PollDownload(DownloadTask, callback); }
private static void OnCheckDownload(Task <DownloadResult> downloadTask, DownloadTextResultHandler callback) { if (downloadTask.IsCompleted) { DownloadResult Result = downloadTask.Result; callback(Result); } else { PollDownload(downloadTask, callback); } }
private static void PollDownload(Task <DownloadResult> downloadTask, DownloadTextResultHandler callback) { Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action <Task <DownloadResult>, DownloadTextResultHandler>(OnCheckDownload), downloadTask, callback); }
private static void OnCheckDownload(Task <Tuple <bool, string?> > downloadTask, string address, DownloadTextResultHandler callback, out bool isFound) { isFound = false; for (; ;) { if (downloadTask.IsCompleted) { Tuple <bool, string?> Result = downloadTask.Result; callback(Result.Item1, Result.Item2); isFound = Result.Item1; string FileName = LocalFileName(address); string FilePath = Path.GetDirectoryName(FileName); if (!File.Exists(FileName)) { if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } using (FileStream Stream = new FileStream(FileName, FileMode.Create, FileAccess.Write)) { using (StreamWriter Writer = new StreamWriter(Stream)) { Writer.Write(Result.Item2); } } } break; } Thread.Sleep(1000); } }