Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- namespace R0409
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- /// <summary>
- /// Constructor de la ventana principal de la aplicación.
- /// </summary>
- public MainWindow()
- {
- InitializeComponent();
- }
- /// <summary>
- /// Responde al clic del botón que invoca a una tarea de forma sincrónica.
- /// </summary>
- /// <param name="sender">Objeto que genera el evento.</param>
- /// <param name="e">Parámetros de la llamada al evento.</param>
- void ButtonSync_Click(object sender, RoutedEventArgs e)
- {
- ContentTextBlock.Text = String.Empty;
- try
- {
- string resultado = Tarea().Result;
- ContentTextBlock.Text = resultado;
- }
- catch (Exception ex)
- {
- ContentTextBlock.Text = ex.InnerException.Message;
- }
- }
- /// <summary>
- /// Responde a clic de llamada a tarea de forma asincrónica.
- /// No tiene acceso al control `ContextTextBlock`.
- /// </summary>
- /// <param name="sender">Objeto que genera el evento.</param>
- /// <param name="e">Parámetros de la llamada al evento.</param>
- void ButtonAsync_Click(object sender, RoutedEventArgs e)
- {
- ContentTextBlock.Text = String.Empty;
- Mouse.OverrideCursor = Cursors.Wait;
- Task<String> tarea = Tarea();
- tarea.ContinueWith(t =>
- {
- ContentTextBlock.Text = t.Exception.InnerException.Message;
- Mouse.OverrideCursor = Cursor;
- }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted,
- TaskScheduler.FromCurrentSynchronizationContext());
- }
- /// <summary>
- /// Responde a clic de llamada a tarea de forma asincrónica.
- /// Tiene acceso al control `ContextTextBlock`.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void ButtonAsyncOK_Click(object sender, RoutedEventArgs e)
- {
- ContentTextBlock.Text = String.Empty;
- Mouse.OverrideCursor = Cursors.Wait;
- Task<String> tarea = Tarea(TaskScheduler.FromCurrentSynchronizationContext());
- tarea.ContinueWith(t => {
- Mouse.OverrideCursor = null;
- ContentTextBlock.Text = tarea.Result;
- },
- CancellationToken.None,
- TaskContinuationOptions.None,
- TaskScheduler.FromCurrentSynchronizationContext());
- }
- Task<String> Tarea()
- {
- return Tarea(TaskScheduler.Default);
- }
- /// <summary>
- /// Método para ejecución asincrónica.
- /// </summary>
- /// <param name="scheduler">Manejador de la ejecución asincrónica.</param>
- /// <returns>Respuesta de la ejecución del thread.</returns>
- Task<String> Tarea(TaskScheduler scheduler)
- {
- Task retraso = Task.Delay(TimeSpan.FromSeconds(5));
- return retraso.ContinueWith( t =>
- {
- string mensaje = String.Format("Tarea ejecutándose en el ID de thread {0}. " +
- "¿Thread en pool de threads?: {1}", Thread.CurrentThread.ManagedThreadId,
- Thread.CurrentThread.IsThreadPoolThread);
- ContentTextBlock.Text = mensaje;
- return mensaje;
- }, scheduler);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement