private void Run() { while (Volatile.Read(ref this.isRunning)) { if (records.Count == 0) { resetEvent.WaitOne(); } if (!Volatile.Read(ref this.isRunning)) { break; } resetEvent.Reset(); var record = records.Dequeue() as DownloadRecord; var serviceClient = new CachingServiceClient(); switch (record.Status) { case ClientDownloadStatus.NotSubmitted: record.Id = serviceClient.StartDownload(record.Address); UpdateState(record, ClientDownloadStatus.Submitted); records.Enqueue(record); break; case ClientDownloadStatus.Submitted: case ClientDownloadStatus.Delegated: case ClientDownloadStatus.Downloading: System.Threading.Thread.Sleep(200); DownloadStatus status = serviceClient.GetDownloadStatus(record.Id); UpdateState(record, status == DownloadStatus.Delegated ? ClientDownloadStatus.Delegated : status == DownloadStatus.Downloading ? ClientDownloadStatus.Downloading : status == DownloadStatus.Downloaded ? ClientDownloadStatus.Downloaded : status == DownloadStatus.Errored ? ClientDownloadStatus.Error : ClientDownloadStatus.Submitted); // Throw it back into the queue for further polling. records.Enqueue(record); break; case ClientDownloadStatus.Downloaded: case ClientDownloadStatus.Error: default: break; } } }
private void Run() { bool isRunning; lock (syncObject) isRunning = this.isRunning; while (isRunning) { if (records.Count == 0) { resetEvent.WaitOne(); } resetEvent.Reset(); var record = records.Dequeue() as DownloadRecord; if (record == null) { continue; } var serviceClient = new CachingServiceClient(); switch (record.Status) { case ClientDownloadStatus.NotSubmitted: int id = serviceClient.StartDownload(record.Address); callingContext.Post(state => { record.Id = (int)state; record.Status = ClientDownloadStatus.Submitted; }, id); records.Enqueue(record); resetEvent.Set(); break; case ClientDownloadStatus.Submitted: case ClientDownloadStatus.Downloading: System.Threading.Thread.Sleep(200); DownloadStatus status = serviceClient.GetDownloadStatus(record.Id); callingContext.Post(state => { var s = (DownloadStatus)state; record.Status = s == DownloadStatus.Downloading ? ClientDownloadStatus.Downloading : s == DownloadStatus.Downloaded ? ClientDownloadStatus.Downloaded : s == DownloadStatus.Errored ? ClientDownloadStatus.Error : ClientDownloadStatus.Submitted; }, status); // Throw it back int the queue for further polling. records.Enqueue(record); resetEvent.Set(); break; case ClientDownloadStatus.Downloaded: case ClientDownloadStatus.Error: default: break; } lock (syncObject) isRunning = this.isRunning; } }