public async Task ProcessFile(BackgroundDownloadFile file) { if (!(await NativeAudioPlayer.VerifyMp3(file.Destination, true))) { LogManager.Shared.Log("Failed Download", file); Download(file); return; } Files.Remove(file.TrackId); if (TasksDictionary.ContainsKey(file.Id)) { TasksDictionary.Remove(file.Id); } var destination = Path.Combine(Locations.MusicDir, Path.GetFileName(file.Destination)); if (File.Exists(destination)) { File.Delete(destination); } File.Move(file.Destination, destination); LogManager.Shared.Log("File Proccessed", file); await OfflineManager.Shared.TrackDownloaded(file.TrackId); NotificationManager.Shared.ProcDownloaderStarted(); }
public string GetScheduleForUser(string userID) { // There's already a schedule, so get it if (SchedulesDictionary.ContainsKey(userID)) { Console.WriteLine(TimeString + " GetSchedule: Already had schedule for user " + userID); return(SchedulesDictionary[userID]); } // If there's no task yet, start one if (!TasksDictionary.ContainsKey(userID)) { Console.WriteLine(TimeString + " GetSchedule: Starting task for user " + userID); var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; TaskCompletionSourcesDictionary.Add(userID, new TaskCompletionSource <bool>()); var task = (new TaskFactory()).StartNew(() => GenerateSchedule(userID, token, TaskCompletionSourcesDictionary[userID]), token); TasksDictionary.Add(userID, task); CancellationTokenSourcesDictionary.Add(userID, tokenSource); Console.WriteLine(TimeString + " GetSchedule: Started task for user " + userID); } // If there's a task running, wait for it Console.WriteLine(TimeString + " GetSchedule: Waiting for first run to complete for user " + userID); var temp = TaskCompletionSourcesDictionary[userID].Task.Result; Console.WriteLine(TimeString + " GetSchedule: First run complete for user " + userID); return(SchedulesDictionary.ContainsKey(userID) ? SchedulesDictionary[userID] : "null"); }
/// <summary> /// Indicates whether a download task is already being downloaded that corresponds to the input task /// </summary> /// <param name="task">The DownloadTask instance</param> /// <returns>True if there is already a download task for the enclosure</returns> public bool IsTaskActive(DownloadTask task) { lock (registry) { if (TasksDictionary.ContainsKey(task.DownloadItem.Enclosure.Url)) { DownloadTask existingTask = TasksDictionary[task.DownloadItem.Enclosure.Url]; return(existingTask.State == DownloadTaskState.Downloading || existingTask.State == DownloadTaskState.Pending); } return(false); } }
/// <summary> /// Registers the task in the storage. /// </summary> /// <param name="task">The DownloadTask instance.</param> public void RegisterTask(DownloadTask task) { lock (registry) { if (!TasksDictionary.ContainsKey(task.DownloadItem.Enclosure.Url)) { TasksDictionary.Add(task.DownloadItem.Enclosure.Url, task); AddTask(task); } else //change state to downloading because we are reattempting { TasksDictionary[task.DownloadItem.Enclosure.Url].State = DownloadTaskState.Pending; } } SaveTask(task); }
/// <summary> /// Removes the task from the storage. /// </summary> /// <param name="task">The DownloadTask.</param> public void UnRegisterTask(DownloadTask task) { lock (registry) { if (TasksDictionary.ContainsKey(task.DownloadItem.Enclosure.Url)) { TasksDictionary.Remove(task.DownloadItem.Enclosure.Url); RemoveTask(task); var disposable = task.Downloader as IDisposable; if (disposable != null) { disposable.Dispose(); } } } string fileName = Path.Combine(RootDir.FullName, task.TaskId + ".task"); FileHelper.DestroyFile(fileName); }