/// <summary> /// Run the background tagger. /// </summary> /// <returns></returns> public Task Run() { TaskFactory tf = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None); CancellationToken cancel = _cancel.Token; return(tf.StartNew(() => { TraceLogger.Log(TraceCategory.Info(), "Background tagging service started"); try { OneNotePageProxy lastPage = null; while (!_jobs.IsCompleted) { TaggingJob j = _jobs.Take(); cancel.ThrowIfCancellationRequested(); try { lastPage = j.Execute(_onenote, lastPage); if (lastPage != null && _jobs.Count == 0) { // no more pending pages - must update the last one and stop carrying forward lastPage.Update(); lastPage = null; } } catch (Exception e) { lastPage = null; TraceLogger.ShowGenericErrorBox("page tagging failed", e); } } } catch (InvalidOperationException) { TraceLogger.Log(TraceCategory.Warning(), "Background tagging job queue depleted"); TraceLogger.Flush(); } catch (OperationCanceledException) { TraceLogger.Log(TraceCategory.Warning(), "Background tagging canceled"); TraceLogger.Flush(); } }, cancel)); }
/// <summary> /// Schedule a tagging job for background operation. /// </summary> /// <param name="job">Tagging job descriptor</param> public void Add(TaggingJob job) { _jobs.Add(job); }