void TaskDownloadQueue() { Thread.CurrentThread.Name = "TaskDownloadThread"; while (!IsDisposed) { while (_downloadQueue.Count > 0) { //查找下一个任务 lock (_downloadQueue) { _downloadTask = _downloadQueue.Count == 0 ? null : _downloadQueue.Dequeue(); if (_downloadTask == null) { continue; } } DownloadQueueItem lvitem = null; this.Invoke(() => { lvitem = queue.Items.Cast <DownloadQueueItem>().FirstOrDefault(x => x.ResourceItem == _downloadTask && x.Status == QueueStatus.Wait); if (lvitem != null) { lvitem.EnsureVisible(); } }); if (lvitem == null) { continue; } lvitem.StartDownload(_downloadTarget, _ => _downloadedHashSet.Contains(_)); } Thread.Sleep(100); } }
public void TestDownloadQueueItem() { // Get the receive queue using (var queue = GetUniqueMessageQueue()) { var queueItem = new DownloadQueueItem( segmentationId: Guid.NewGuid().ToString(), modelId: Guid.NewGuid().ToString(), resultsDirectory: CreateTemporaryDirectory().FullName, referenceDicomFiles: new[] { new byte[] { 5, 6, 8, 10 }, new byte[] { 2, 9, 11, 22 } }, calledApplicationEntityTitle: "Test2", callingApplicationEntityTitle: "Test30", destinationApplicationEntity: new GatewayApplicationEntity("Test3", 105, "Test4"), tagReplacementJsonString: "HELLO WORLD 1 / 2 3; 5", associationGuid: Guid.NewGuid(), associationDateTime: DateTime.UtcNow, isDryRun: false) { DequeueCount = 4 }; TransactionalEnqueue(queue, queueItem); var item = TransactionalDequeue <DownloadQueueItem>(queue); Assert.IsNotNull(item); AssertAllProperties(queueItem, item); } }
/// <summary> /// Queues addon for download /// </summary> public static void Queue(AddonDisplayObject addon, DownloadProgressChangedEventHandler ProgressCallback = null) { DownloadQueueItem item = new DownloadQueueItem { ProgressCallback = ProgressCallback ?? Manager_DownloadProgressChanged, addon = addon }; DownloadQueue.Add(item); ProcessQueue(); }
/// <summary> /// Shorthand for creating an association status log entry. /// </summary> /// <param name="associationStatus">Association status.</param> /// <param name="information">Freeform information about log item.</param> /// <param name="queueItemBase">Queue item base.</param> /// <param name="deleteQueueItem">Delete queue item.</param> /// <param name="downloadQueueItem">Download queue item.</param> /// <param name="downloadProgress">Download progress.</param> /// <param name="downloadError">Download error.</param> /// <param name="pushQueueItem">Push queue item.</param> /// <param name="uploadQueueItem">Upload queue item.</param> /// <param name="segmentationId">Segmentation id.</param> /// <param name="modelId">Model id.</param> /// <param name="destination">Destination.</param> /// <param name="path">Path.</param> /// <param name="failedDicomTags">String formatted list of failed DICOM tags.</param> /// <param name="dicomDataReceiverProgress">Receiver progress.</param> /// <returns>New LogEntry of type SegmentationStatus.</returns> public static LogEntry Create( AssociationStatus associationStatus, string information = null, QueueItemBase queueItemBase = null, DeleteQueueItem deleteQueueItem = null, DownloadQueueItem downloadQueueItem = null, int?downloadProgress = null, string downloadError = null, PushQueueItem pushQueueItem = null, UploadQueueItem uploadQueueItem = null, string segmentationId = null, string modelId = null, (string ipAddress, string title, int port)?destination = null,
public void AddResourceToQueue(params IResourceInfo[] resources) { if (resources == null || resources.Length == 0) { return; } if (string.IsNullOrEmpty(_downloadTarget)) { _saveTorrent.FileName = resources[0].Title + ".torrent"; if (_saveTorrent.ShowDialog() != DialogResult.OK) { return; } _downloadTarget = Path.GetDirectoryName(_saveTorrent.FileName); } var items = resources.Where(s => !_queueHash.Contains(s.Hash)).Select(s => { var item = new DownloadQueueItem(s); item.StatusChanged += (x, y) => { var si = item; if (si.Status == QueueStatus.Succeed || si.Status == QueueStatus.Failed || si.Status == QueueStatus.Skipped) { //成功 RefreshTaskQueueStatus(si); } else if (si.Status == QueueStatus.Running) { RefreshTaskProgress(si); } }; item.EngineIndexChanged += (x, y) => { this.Invoke(() => { stProgressCurrent.Value = (x as DownloadQueueItem).EngineIndex; }); }; return((ListViewItem)item); }).ToArray(); queue.Items.AddRange(items); lock (_downloadQueue) { _downloadQueue.EnqueueMany(resources); } }
public void AddFile(Uri sourceUri, string targetFilePath, object cookie, HashCollection hashCollection, int maxFileSize) { UriHelper.ValidateSupportedScheme(sourceUri); DownloadQueueItem item = new DownloadQueueItem { _sourceUri = sourceUri, _targetPath = targetFilePath, _cookie = cookie, _hashCollection = hashCollection, _maxFileSize = maxFileSize }; lock (this._fileQueue) { this._fileQueue.Enqueue(item); this._eventArgs._filesTotal++; } }
void RefreshTaskProgress(DownloadQueueItem item) { if (InvokeRequired) { Invoke(new Action <DownloadQueueItem>(RefreshTaskProgress), item); return; } stStatus.Text = string.Format("开始下载 【{0}】 ...", item.ResourceItem.Title.GetSubString(40)); stProgressCurrent.Visible = true; stStatus.Image = Properties.Resources._16px_loading_1; var total = queue.Items.Count; var processed = total - _downloadQueue.Count; if (stProgressTotal.Value > total) { stProgressTotal.Value = 0; } stProgressTotal.Maximum = total; stProgressTotal.Value = processed; stDownloadProgressTotal.Text = string.Format("{0}/{1}", processed, total); }
void RefreshTaskQueueStatus(DownloadQueueItem item) { if (InvokeRequired) { Invoke(new Action <DownloadQueueItem>(RefreshTaskQueueStatus), item); return; } if (item.Status == QueueStatus.Succeed) { _downloadedHashSet.SafeAdd(item.Hash); var mstTitle = "种子下载成功"; var mstContent = string.Format("种子【{0}】下载成功!", item.ResourceItem.Title); if (AppContext.Instance.Options.UsingFloatTip) { ResourceOperation.MainForm.ShowFloatTip(mstTitle + "\n" + mstContent); } else { ni.ShowBalloonTip(3000, mstTitle, mstContent, ToolTipIcon.Info); } if (AppContext.Instance.Options.EnableAutoMark) { AppContext.Instance.ResourceOperation.SetTorrentMask(AppContext.Instance.Options.AutoMarkDownloadedTorrent, item.ResourceItem); } AppContext.Instance.ResourceOperation.MarkDone(item.ResourceItem); } else if (item.Status == QueueStatus.Skipped) { //下载跳过 if (AppContext.Instance.Options.EnableAutoMark) { AppContext.Instance.ResourceOperation.SetTorrentMask(AppContext.Instance.Options.AutoMarkDownloadedTorrent, item.ResourceItem); } AppContext.Instance.DownloadHistory.AddOrUpdate(item.ResourceItem.Hash, new HistoryItem() { Title = item.ResourceItem.Title, DownloadTime = DateTime.Now }); } else { var mstTitle = "种子下载失败"; var mstContent = string.Format("种子【{0}】下载失败!请使用磁力链来获得资源地址!", item.ResourceItem.Title); if (AppContext.Instance.Options.UsingFloatTip) { ResourceOperation.MainForm.ShowFloatTip(mstTitle + "\n" + mstContent); } else { ni.ShowBalloonTip(3000, mstTitle, mstContent, ToolTipIcon.Warning); } } stProgressCurrent.Visible = false; stStatus.Text = "当前没有任务等待下载"; stStatus.Image = Properties.Resources.tick_16; }
private bool PatchSingleFile(DownloadQueueItem item, Hashtable dependencyTable) { if (item._hashCollection == null) { return false; } string location = null; using (HashCollection.HashEnumerator enumerator = item._hashCollection.GetEnumerator()) { while (enumerator.MoveNext()) { string compositString = enumerator.Current.CompositString; if (dependencyTable.Contains(compositString)) { location = (string) dependencyTable[compositString]; goto Label_0062; } } } Label_0062: if (location == null) { return false; } if (this._fCancelPending) { return false; } if (!FileHashVerified(item._hashCollection, location)) { Logger.AddInternalState("Hash verify failed for " + location + ", not using it for file patching."); return false; } FileStream patchSourceStream = null; FileStream patchTargetStream = null; try { patchSourceStream = GetPatchSourceStream(location); if (patchSourceStream == null) { return false; } Directory.CreateDirectory(Path.GetDirectoryName(item._targetPath)); patchTargetStream = GetPatchTargetStream(item._targetPath); if (patchTargetStream == null) { return false; } this._eventArgs._fileSourceUri = item._sourceUri; this._eventArgs.FileLocalPath = item._targetPath; this._eventArgs.Cookie = null; this._eventArgs._fileResponseUri = null; this.CheckForSizeLimit((ulong) patchSourceStream.Length, true); this._accumulatedBytesTotal += patchSourceStream.Length; this.SetBytesTotal(); this.OnModified(); int count = 0; int tickCount = Environment.TickCount; patchTargetStream.SetLength(patchSourceStream.Length); patchTargetStream.Position = 0L; do { if (this._fCancelPending) { return false; } count = patchSourceStream.Read(this._buffer, 0, this._buffer.Length); if (count > 0) { patchTargetStream.Write(this._buffer, 0, count); } this._eventArgs._bytesCompleted += count; this._eventArgs._progress = (int) ((this._eventArgs._bytesCompleted * 100L) / this._eventArgs._bytesTotal); this.OnModifiedWithThrottle(ref tickCount); } while (count > 0); } finally { if (patchSourceStream != null) { patchSourceStream.Close(); } if (patchTargetStream != null) { patchTargetStream.Close(); } } this._eventArgs.Cookie = item._cookie; this._eventArgs._filesCompleted++; this.OnModified(); DownloadResult result = new DownloadResult { ResponseUri = null }; this._downloadResults.Add(result); Logger.AddInternalState(item._targetPath + " is patched from store."); return true; }
private bool PatchSingleFile(DownloadQueueItem item, Hashtable dependencyTable) { if (item._hashCollection == null) { return(false); } string location = null; using (HashCollection.HashEnumerator enumerator = item._hashCollection.GetEnumerator()) { while (enumerator.MoveNext()) { string compositString = enumerator.Current.CompositString; if (dependencyTable.Contains(compositString)) { location = (string)dependencyTable[compositString]; goto Label_0062; } } } Label_0062: if (location == null) { return(false); } if (this._fCancelPending) { return(false); } if (!FileHashVerified(item._hashCollection, location)) { Logger.AddInternalState("Hash verify failed for " + location + ", not using it for file patching."); return(false); } FileStream patchSourceStream = null; FileStream patchTargetStream = null; try { patchSourceStream = GetPatchSourceStream(location); if (patchSourceStream == null) { return(false); } Directory.CreateDirectory(Path.GetDirectoryName(item._targetPath)); patchTargetStream = GetPatchTargetStream(item._targetPath); if (patchTargetStream == null) { return(false); } this._eventArgs._fileSourceUri = item._sourceUri; this._eventArgs.FileLocalPath = item._targetPath; this._eventArgs.Cookie = null; this._eventArgs._fileResponseUri = null; this.CheckForSizeLimit((ulong)patchSourceStream.Length, true); this._accumulatedBytesTotal += patchSourceStream.Length; this.SetBytesTotal(); this.OnModified(); int count = 0; int tickCount = Environment.TickCount; patchTargetStream.SetLength(patchSourceStream.Length); patchTargetStream.Position = 0L; do { if (this._fCancelPending) { return(false); } count = patchSourceStream.Read(this._buffer, 0, this._buffer.Length); if (count > 0) { patchTargetStream.Write(this._buffer, 0, count); } this._eventArgs._bytesCompleted += count; this._eventArgs._progress = (int)((this._eventArgs._bytesCompleted * 100L) / this._eventArgs._bytesTotal); this.OnModifiedWithThrottle(ref tickCount); }while (count > 0); } finally { if (patchSourceStream != null) { patchSourceStream.Close(); } if (patchTargetStream != null) { patchTargetStream.Close(); } } this._eventArgs.Cookie = item._cookie; this._eventArgs._filesCompleted++; this.OnModified(); DownloadResult result = new DownloadResult { ResponseUri = null }; this._downloadResults.Add(result); Logger.AddInternalState(item._targetPath + " is patched from store."); return(true); }
private void PatchFiles(SubscriptionState subState) { if (!subState.IsInstalled) { Logger.AddInternalState("Subscription is not installed. No patching."); } else { System.Deployment.Internal.Isolation.Store.IPathLock @lock = null; System.Deployment.Internal.Isolation.Store.IPathLock lock2 = null; using (subState.SubscriptionStore.AcquireSubscriptionReaderLock(subState)) { if (!subState.IsInstalled) { Logger.AddInternalState("Subscription is not installed. No patching."); return; } Hashtable hashtable = new Hashtable(); try { @lock = subState.SubscriptionStore.LockApplicationPath(subState.CurrentBind); AddFilesInHashtable(hashtable, subState.CurrentApplicationManifest, @lock.Path); try { if (subState.PreviousBind != null) { lock2 = subState.SubscriptionStore.LockApplicationPath(subState.PreviousBind); AddFilesInHashtable(hashtable, subState.PreviousApplicationManifest, lock2.Path); } Queue queue = new Queue(); do { DownloadQueueItem item = null; lock (this._fileQueue) { if (this._fileQueue.Count > 0) { item = (DownloadQueueItem)this._fileQueue.Dequeue(); } } if (item == null) { break; } if (!this.PatchSingleFile(item, hashtable)) { queue.Enqueue(item); } }while (!this._fCancelPending); lock (this._fileQueue) { while (this._fileQueue.Count > 0) { queue.Enqueue(this._fileQueue.Dequeue()); } this._fileQueue = queue; } } finally { if (lock2 != null) { lock2.Dispose(); } } } finally { if (@lock != null) { @lock.Dispose(); } } } if (this._fCancelPending) { throw new DownloadCancelledException(); } } }