Curso C#. Cancelación de Task. Vídeo 117

Curso C#. Cancelación de Task. Vídeo 117

New Section

In this section, the speaker introduces the topic of canceling tasks in Sisal and explains the process using cancellation tokens.

Introduction to Canceling Tasks

  • When a task needs to be canceled while it is running, a cancellation token is created to notify the task that it can be canceled under certain conditions.
  • To create a cancellation token, two classes are used: CancellationSource and CancellationToken.
  • It is recommended to refer to the official documentation for these classes as they provide detailed examples on creating a cancellation token.

Exploring Cancellation Tokens

This part delves into understanding how cancellation tokens work and their significance in task management.

Understanding Cancellation Tokens

  • The CancellationToken class signals an object that should be canceled, while the CancellationToken class propagates notifications for operations to be canceled.
  • Exploring the official documentation and example provided helps in grasping the concept of creating a cancellation token effectively.

Implementing Task Cancellation

Here, a simplified example demonstrates how to cancel a task based on specific conditions within a program.

Task Cancellation Example

  • A program is illustrated where tasks store random numbers in lists, with the condition that if zero is generated, the task should be canceled.
  • A straightforward program incrementing an accumulator variable within a loop showcases how tasks can be managed and potentially canceled based on external conditions.

Propagating Task Cancellation

This segment focuses on canceling tasks based on external factors by utilizing cancellation tokens effectively.

Propagation of Task Cancellation

Understanding Cancellation Tokens in Programming

In this section, the speaker explains the concept of cancellation tokens in programming and how they are utilized to interrupt tasks.

Creating a Cancellation Token Object

  • A new cancellation token object is created to signal which token should interrupt the task.
  • The created cancellation token points to the specific token that cancels the task.

Propagating Cancellation

  • To prepare a method for potential cancellation, it must be able to receive a cancellation token as an argument.
  • Methods need to be adjusted to accept a cancellation token, indicating that under certain conditions, the task could be canceled.

Setting Cancellation Conditions

  • Defining conditions for task cancellation; for instance, canceling if a variable exceeds a certain value.
  • Demonstrating setting up a loop where an accumulator variable increments until it meets the cancelation condition.

Implementing Task Cancellation Logic

This part delves into implementing logic for canceling tasks based on specified conditions within programming tasks.

Incrementing Accumulator Variable

  • Incrementing the accumulator variable within a loop from both the main thread and another thread simultaneously.

Triggering Task Cancellation

  • Checking if the accumulator value surpasses 100 during each iteration and triggering task cancellation accordingly.

Handling Task Termination

  • Using the name of the cancellation token to propagate task cancellation effectively.
  • Ensuring proper termination of both the canceled task and main thread execution flow using break statements.

Finalizing Task Execution and Result Display

Concluding steps involve finalizing task execution, displaying results, and ensuring proper program termination upon task cancelation.

Finalizing Task Execution

  • Instructing methods handling primary tasks to exit their execution flow upon receiving a cancel request through conditional statements.

Program Termination Upon Cancelation

  • Specifying actions for method termination upon receiving a cancel request using return statements.

Observing Results Post-Cancelation

How to Cancel a Task in Programming

In this section, the speaker discusses the process of canceling a task in programming and the importance of handling task cancellation effectively.

Handling Task Cancellation

  • When canceling a task, if it is left incomplete, there should be mechanisms to handle reverting any changes made by the task.
  • To reset a task that has been canceled, ensure that any modifications made by the task are undone. In this case, setting the accumulator variable back to zero is crucial.
  • Implement steps in your program to revert any changes made by a canceled task effectively. This ensures that the program returns to its initial state after cancellation.

Ensuring Task Completion and Reversion

This section emphasizes the significance of ensuring complete execution or proper reversion when canceling tasks in programming.

Completing or Reverting Tasks

  • It is essential to manage tasks such that they either complete successfully or revert all changes if canceled midway through execution.
  • Properly handling cancellation involves creating a cancellation token, propagating it through methods, and defining actions for tasks upon receiving a cancellation request.

Maintaining Program Flow

The discussion shifts towards maintaining program flow when dealing with cancellations and ensuring tasks are executed as intended.

Program Continuity

  • Even with cancellation mechanisms in place, tasks will proceed normally if not interrupted by cancellation requests during execution.
  • By managing cancellations effectively within loops or processes, programs can maintain their functionality without disruptions caused by premature terminations.

Closing Remarks and Course Offerings

Concluding thoughts on effective task management and an invitation to explore additional educational resources offered by the speaker.

Educational Resources

Video description

En este vídeo vemos cómo cancelar Tasks. Si ocurre algo en nuestro programa que deba cancelar una Task, hay que avisar a esa Task de que debe cancelarse y dejar la tarea como estaba antes de comenzar. Para más cursos, ejercicios y manuales visita: https://www.pildorasinformaticas.es