private bool FindAndDownload() { var result = false; lock (this) { var download = GetNextAvailableItemToDownload(); if (download != null) { var id = Utility.GetMacAddress(); #if DEBUG id = "0123456789"; #endif IMediaActivities activities; download.PropertyChanged += (sender, args) => { _downloadDataChanged = true; }; _downloader = CreateDownloader(); try { ActivitiesFactory.A<IMediaActivities>().DownloadStarted(download); _downloader.Download(download); result = true; ActivitiesFactory.A<IMediaActivities>().DownloadStopped(download); } catch (DeviceLocateException exception) { UpdateDownloadStoreWithError(download, string.Format("Cannot find the Media Server '{0}'", exception.DeviceName)); Logger.TraceErr(exception); } catch (PathLocateException exception) { var title = exception.Path.Split('\\').Last(); UpdateDownloadStoreWithError(download, string.Format("Cannot find media '{0}'", title)); Logger.TraceErr(exception); } catch (DownloadInterruptedException exception) { UpdateDownloadStoreWithError(download, string.Format("Error: {0}", exception.Message)); Logger.TraceErr(exception); } catch (Exception exception) { Logger.TraceErr(exception); } finally { CleanupDownloader(_downloader); CleanupDownload(download); _downloader = null; } } } return result; }
public void Stop() { _watcher.EnableRaisingEvents = false; Factory.InterruptConversionProcess = true; if (_runDownloaderThread != null) { _done = true; if (_downloader != null) { try { _downloader.Cancel(); } catch (Exception exception) { // catch error if _downloader becomes null unexpectedly Logger.TraceErr(exception); } } _runDownloaderThread.Join(TimeSpan.FromSeconds(15D)); _runDownloaderThread = null; _downloader = null; } if (_watcherPulseThread != null) { _watcherPulseThread.Join(TimeSpan.FromSeconds(1D)); _watcherPulseThread = null; } Utility.ClearAllDownloads(); IsStarted = false; }
private void CleanupDownloader(IDownloadFromPath downloader) { downloader.DownloadingStopped -= DownloadingStopped; downloader.GetDevice -= GetDevice; downloader.SaveProgress -= SaveDownloadQueueStatus; }