/// <summary> /// Adds a new job to the tracker. /// </summary> /// <param name="ticket">The <see cref="IJobTicket"/> to begin /// tracking.</param> /// <param name="handler">The <see cref="IJobResultsHandler"/> called /// when the job has finished executing.</param> public void Add(IJobTicket ticket, IJobResultsHandler handler) { JobViewModel vm = new JobViewModel(ticket); vm.Handler = handler; switch (ticket.State) { case JobState.InQueue: vm.JobStarted += _jobStarted; vm.JobFinished += _jobFinished; _safeViewModelAction(Pending.Add, vm); break; case JobState.Running: vm.JobStarted += _jobStarted; vm.JobFinished += _jobFinished; Current = vm; break; default: _safeViewModelAction(Finished.Add, vm); break; } }
/// <summary> /// Occurs when a contained JobViewModel has finished /// </summary> /// <param name="sender">The JobViewModel</param> /// <param name="e">N/A</param> private void _jobFinished(object sender, EventArgs e) { Current = null; _safeViewModelAction(x => Pending.Remove(x), (JobViewModel)sender); _safeViewModelAction(Finished.Add, (JobViewModel)sender); }
/// <summary> /// Performs an action with a view model on the UI thread /// </summary> /// <param name="action">The action to perform</param> /// <param name="item">The view model to perform the action with</param> private void _safeViewModelAction(Action <JobViewModel> action, JobViewModel item) { SendOrPostCallback c = x => action(item); _context.Context.Send(c, null); }
/// <summary> /// Occurs when a contained JobViewModel has begun /// </summary> /// <param name="sender">The JobViewModel</param> /// <param name="e">N/A</param> private void _jobStarted(object sender, EventArgs e) { Current = (JobViewModel)sender; _safeViewModelAction(x => Pending.Remove(x), Current); }