/// <summary> /// Synchronously adds the <paramref name="cameraTask"/> given to an execution queue /// and returns awaitable task to wait for the <paramref name="cameraTask"/> completion. /// </summary> /// <param name="cameraTask">Task to enqueue.</param> /// <returns>Awaitable task.</returns> /// <exception cref="ArgumentNullException"><paramref name="cameraTask"/> is <see langword="null"/>.</exception> public Task EnqueueTaskAsync(ICameraTask cameraTask) { if (cameraTask == null) { throw new ArgumentNullException("cameraTask"); } QueuedCameraTask task = this.AddTaskToQueue(cameraTask); return(Task.Run(() => task.WaitForComplete(Timeout.InfiniteTimeSpan))); }
/// <summary> /// Adds the <paramref name="cameraTask"/> specified to the execution queue. /// </summary> /// <param name="cameraTask">The camera task to add to the queue.</param> /// <returns>Execution wrapper for the <paramref name="cameraTask"/> given.</returns> private QueuedCameraTask AddTaskToQueue(ICameraTask cameraTask) { QueuedCameraTask queuedTask = new QueuedCameraTask(cameraTask); lock (this.syncRoot) { this.pendingTasks.Add(queuedTask); this.ScheduleNextTask(); } return(queuedTask); }
/// <summary> /// Schedules the next pending task for execution, if needed. /// </summary> private void ScheduleNextTask() { lock (this.syncRoot) { if (this.currentTask != null || !this.pendingTasks.Any()) { return; } this.currentTask = this.pendingTasks[0]; this.currentTask.Task.Complete += this.CameraTaskCompleted; this.pendingTasks.RemoveAt(0); ThreadPool.QueueUserWorkItem(state => this.StartCurrentTask()); } }
/// <summary> /// The <see cref="ICameraTask.Complete"/> event handler. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void CameraTaskCompleted(object sender, EventArgs e) { lock (this.syncRoot) { // Makes sure that the current method isn't called for the second // time for the same task. if (this.currentTask == null || this.currentTask.Task != sender) { return; } this.currentTask.Task.Complete -= this.CameraTaskCompleted; this.currentTask.Dispose(); this.currentTask = null; this.ScheduleNextTask(); } }
/// <summary> /// Adds the <paramref name="cameraTask"/> specified to the execution queue. /// </summary> /// <param name="cameraTask">The camera task to add to the queue.</param> /// <returns>Execution wrapper for the <paramref name="cameraTask"/> given.</returns> private QueuedCameraTask AddTaskToQueue(ICameraTask cameraTask) { QueuedCameraTask queuedTask = new QueuedCameraTask(cameraTask); lock (this.syncRoot) { this.pendingTasks.Add(queuedTask); this.ScheduleNextTask(); } return queuedTask; }