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> /// 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); }