private void InternalAddItem(List <FrameInfo> listFrames, Parameters param) { //Creates the Cancellation Token var cancellationTokenSource = new CancellationTokenSource(); CancellationTokenList.Add(cancellationTokenSource); var context = TaskScheduler.FromCurrentSynchronizationContext(); //Creates Task and send the Task Id. var a = -1; var task = new Task(() => { Encode(listFrames, a, param, cancellationTokenSource); }, CancellationTokenList.Last().Token, TaskCreationOptions.LongRunning); a = task.Id; #region Error Handling task.ContinueWith(t => { var aggregateException = t.Exception; aggregateException?.Handle(exception => true); SetStatus(Status.Error, a, null, false, t.Exception); LogWriter.Log(t.Exception, "Encoding Error"); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, context); #endregion #region Creates List Item var encoderItem = new EncoderListViewItem { OutputType = param.Type, Text = FindResource("Encoder.Starting").ToString(), FrameCount = listFrames.Count, Id = a, TokenSource = cancellationTokenSource }; encoderItem.CancelClicked += EncoderItem_CancelClicked; EncodingListView.Items.Add(encoderItem); EncodingListView.ScrollIntoView(encoderItem); #endregion try { TaskList.Add(task); TaskList.Last().Start(); } catch (Exception ex) { Dialog.Ok("Task Error", "Unable to start the encoding task", "A generic error occured while trying to start the encoding task. " + ex.Message); LogWriter.Log(ex, "Errow while starting the task."); } }
private void InternalAddItem(List <FrameInfo> listFrames, string fileName, Export type) { //Creates the Cancellation Token var cancellationTokenSource = new CancellationTokenSource(); CancellationTokenList.Add(cancellationTokenSource); var context = TaskScheduler.FromCurrentSynchronizationContext(); //Creates Task and send the Task Id. int a = -1; var task = new Task(() => { Encode(listFrames, a, fileName, type, cancellationTokenSource); }, CancellationTokenList.Last().Token, TaskCreationOptions.LongRunning); a = task.Id; #region Error Handling task.ContinueWith(t => { AggregateException aggregateException = t.Exception; if (aggregateException != null) { aggregateException.Handle(exception => true); } SetStatus(Status.Error, a); LogWriter.Log(t.Exception, "Encoding Error"); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, context); #endregion #region Creates List Item var encoderItem = new EncoderListViewItem { Image = type == Export.Gif ? (UIElement)FindResource("Vector.Image") : (UIElement)FindResource("Vector.Video"), Text = "Starting", FrameCount = listFrames.Count, Id = a, TokenSource = cancellationTokenSource, }; encoderItem.CloseButtonClickedEvent += EncoderItem_CloseButtonClickedEvent; encoderItem.LabelLinkClickedEvent += EncoderItem_LabelLinkClickedEvent; EncodingListBox.Items.Add(encoderItem); #endregion try { TaskList.Add(task); TaskList.Last().Start(); } catch (Exception ex) { Dialog.Ok("Task Error", "Unable to start the encoding task", "A generic error occured while trying to start the encoding task. " + ex.Message); LogWriter.Log(ex, "Errow while starting the task."); } }