Fhernd

FormularioConAsync.cs

Sep 9th, 2014
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5.  
  6. namespace Articulos.Cap04
  7. {
  8.     ///<summary>
  9.     /// Representa el formulario para el 'dibujo' de la interfaz.
  10.     ///</summary>
  11.     public partial class FormularioConAsync : Form
  12.     {
  13.         System.ComponentModel.Container components = null;
  14.  
  15.         ///<summary>
  16.         /// Constructor sin-argumentos.
  17.         ///</summary>
  18.         public FormularioConAsync()
  19.         {
  20.             InitializeComponents();
  21.         }
  22.  
  23.         ///<summary>
  24.         /// Uso del patrón Disposable para la liberación 'responsable' de recursos del sistema.
  25.         ///</summary>
  26.         protected override void Dispose(bool disposing)
  27.         {
  28.             if (disposing)
  29.             {
  30.                 if (components != null)
  31.                 {
  32.                     components.Dispose();
  33.                 }
  34.             }
  35.  
  36.             base.Dispose(disposing);
  37.         }
  38.  
  39.         #region COMMON_CODE
  40.         ///<summary>
  41.         /// Iniciliaza algunas de las propiedades del formulario, e
  42.         /// invoca los métodos auxiliares para la construcción de la interfaz
  43.         // de usuario.
  44.         ///</summary>
  45.         protected void InitializeComponents()
  46.         {
  47.             this.Font = new Font("Segoe UI", 9);
  48.             this.FormBorderStyle = FormBorderStyle.FixedSingle;
  49.             this.MaximizeBox = false;
  50.             this.Name = "frmAlarmClockGui";
  51.             this.Size = new Size(500, 100);
  52.             this.Text = "Uso Asincrónico de Eventos con Expresioens Lambda";
  53.  
  54.             Label lblNumero = new Label();
  55.             lblNumero.AutoSize = true;
  56.             lblNumero.Location = new Point(3, 17);
  57.             lblNumero.Text = "Número: ";
  58.  
  59.             TextBox txtNumero = new TextBox();
  60.             txtNumero.Location = new Point (61, 13);
  61.             txtNumero.TabIndex = 1;
  62.  
  63.             Label lblResultado = new Label();
  64.             lblResultado.AutoSize = true;
  65.             lblResultado.Location = new Point (3, 41);
  66.  
  67.             Button btnCalcularFibonacci = new Button ();
  68.             btnCalcularFibonacci.AutoSize = true;
  69.             btnCalcularFibonacci.Text = "Calcular Fibonacci";
  70.             btnCalcularFibonacci.Location = new Point (169, 11);
  71.             btnCalcularFibonacci.TabIndex = 2;
  72.  
  73.             // El evento `Click` de `Button` se puede invocar de
  74.             // forma asincrona:
  75.             btnCalcularFibonacci.Click += async (sender, e) =>
  76.             {
  77.                 Task<long> tarea = CalculoFibonacci (long.Parse(txtNumero.Text));
  78.  
  79.                 long fibonacci = await tarea;
  80.  
  81.                 lblResultado.Text = String.Format("Resultado: {0}", fibonacci.ToString());
  82.             };
  83.  
  84.             // Botón para mostrar otro diálogo:
  85.             Button btnMostrarMensaje = new Button();
  86.             btnMostrarMensaje.AutoSize = true;
  87.             btnMostrarMensaje.Text = "Mostrar Mensaje";
  88.             btnMostrarMensaje.Location = new Point(291, 11);
  89.             btnMostrarMensaje.TabIndex = 3;
  90.             // El evento `Click` de `btnMostrarMensaje` puede ser
  91.             // invocado en cualquier instante:
  92.             btnMostrarMensaje.Click += (sender, e) => MessageBox.Show("La interfaz no se congela mientras se calcula la serie Fibonacci.");
  93.  
  94.             this.Controls.Add(btnCalcularFibonacci);
  95.             this.Controls.Add(btnMostrarMensaje);
  96.             this.Controls.Add(lblNumero);
  97.             this.Controls.Add(txtNumero);
  98.             this.Controls.Add(lblResultado);
  99.         }
  100.  
  101.         private async Task<long> CalculoFibonacci(long numero)
  102.         {
  103.             long fibonacci = 1;
  104.  
  105.             for (long i = 1; i < numero; ++i)
  106.             {
  107.                 fibonacci *= i;
  108.             }
  109.  
  110.             // Retardo arbitrario (demostración):
  111.             await Task.Delay (3100);
  112.            
  113.             return fibonacci;
  114.         }
  115.  
  116.         #endregion COMMON_CODE
  117.  
  118.         #region CLIENT_CODE
  119.         ///<summary>
  120.         /// Código cliente para administrar la ejecución de la aplicación.
  121.         ///</summary>
  122.         public static void Main()
  123.         {
  124.             Application.EnableVisualStyles();
  125.             Application.Run(new FormularioConAsync());
  126.         }
  127.         #endregion
  128.     }
  129. }
Add Comment
Please, Sign In to add comment