private void DoWork() { while (true) { if (_progressHost.CancelRequested) { throw new OperationCancelledException(); } bool success; DownloadWorkItem workItem = (DownloadWorkItem)_downloadQueue.TryDequeue(out success); if (!success) { return; // no more work in queue; this thread is done } try { workItem.Download(); } catch (Exception e) { Trace.WriteLine("Error downloading link while importing favorites: " + e.ToString()); } } }
public DownloadResults Download() { TickableProgressTick tickableProgress = new TickableProgressTick(_progressHost, _urlsToDownload.Count); Hashtable workItems = new Hashtable(); foreach (string url in _urlsToDownload.Keys) { DownloadWorkItem workItem = new DownloadWorkItem(url, (int)_urlsToDownload[url], tickableProgress); workItems.Add(url, workItem); _downloadQueue.Enqueue(workItem); } ParallelExecution execution = new ParallelExecution(new ThreadStart(DoWork), _threadCount); execution.Execute(); DownloadResults results = new DownloadResults(); foreach (string url in workItems.Keys) { results.AddResult(url, ((DownloadWorkItem)workItems[url]).FilePath); } return(results); }
/// <summary> /// The work to be performed by each worker thread. /// </summary> private void WorkerThreadStart() { while (true) { bool success; DownloadWorkItem workItem = (DownloadWorkItem)workQueue.TryDequeue(out success); if (!success) { return; // no more work in queue; this thread is done } try { workItem.progressHost.UpdateProgress(string.Format(CultureInfo.CurrentCulture, Res.Get(StringId.ProgressDownloading), workItem.reference.AbsoluteUrl)); DownloadReference(workItem.reference, workItem.siteStorage, new ProgressHostFilter(workItem.progressHost, ProgressHostFilter.MessageType.UpdateMessage)); } catch (Exception e) { HandleException(e); } } }
public DownloadResults Download() { TickableProgressTick tickableProgress = new TickableProgressTick(_progressHost, _urlsToDownload.Count); Hashtable workItems = new Hashtable(); foreach (string url in _urlsToDownload.Keys) { DownloadWorkItem workItem = new DownloadWorkItem(url, (int)_urlsToDownload[url], tickableProgress); workItems.Add(url, workItem); _downloadQueue.Enqueue(workItem); } ParallelExecution execution = new ParallelExecution(new ThreadStart(DoWork), _threadCount); execution.Execute(); DownloadResults results = new DownloadResults(); foreach (string url in workItems.Keys) { results.AddResult(url, ((DownloadWorkItem)workItems[url]).FilePath); } return results; }