Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 12, 2014 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/0c93e01b7230e726bcda to your computer and use it in GitHub Desktop.
Save Fhernd/0c93e01b7230e726bcda to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Articulos.Cap04
{
///<summary>
/// Representa el formulario para el 'dibujo' de la interfaz.
///</summary>
public partial class FormularioConAsync : Form
{
System.ComponentModel.Container components = null;
///<summary>
/// Constructor sin-argumentos.
///</summary>
public FormularioConAsync()
{
InitializeComponents();
}
///<summary>
/// Uso del patrón Disposable para la liberación 'responsable' de recursos del sistema.
///</summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region COMMON_CODE
///<summary>
/// Iniciliaza algunas de las propiedades del formulario, e
/// invoca los métodos auxiliares para la construcción de la interfaz
// de usuario.
///</summary>
protected void InitializeComponents()
{
this.Font = new Font("Segoe UI", 9);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmAlarmClockGui";
this.Size = new Size(500, 100);
this.Text = "Uso Asincrónico de Eventos con Expresioens Lambda";
Label lblNumero = new Label();
lblNumero.AutoSize = true;
lblNumero.Location = new Point(3, 17);
lblNumero.Text = "Número: ";
TextBox txtNumero = new TextBox();
txtNumero.Location = new Point (61, 13);
txtNumero.TabIndex = 1;
Label lblResultado = new Label();
lblResultado.AutoSize = true;
lblResultado.Location = new Point (3, 41);
Button btnCalcularFibonacci = new Button ();
btnCalcularFibonacci.AutoSize = true;
btnCalcularFibonacci.Text = "Calcular Fibonacci";
btnCalcularFibonacci.Location = new Point (169, 11);
btnCalcularFibonacci.TabIndex = 2;
// El evento `Click` de `Button` se puede invocar de
// forma asincrona:
btnCalcularFibonacci.Click += async (sender, e) =>
{
Task<long> tarea = CalculoFibonacci (long.Parse(txtNumero.Text));
long fibonacci = await tarea;
lblResultado.Text = String.Format("Resultado: {0}", fibonacci.ToString());
};
// Botón para mostrar otro diálogo:
Button btnMostrarMensaje = new Button();
btnMostrarMensaje.AutoSize = true;
btnMostrarMensaje.Text = "Mostrar Mensaje";
btnMostrarMensaje.Location = new Point(291, 11);
btnMostrarMensaje.TabIndex = 3;
// El evento `Click` de `btnMostrarMensaje` puede ser
// invocado en cualquier instante:
btnMostrarMensaje.Click += (sender, e) => MessageBox.Show("La interfaz no se congela mientras se calcula la serie Fibonacci.");
this.Controls.Add(btnCalcularFibonacci);
this.Controls.Add(btnMostrarMensaje);
this.Controls.Add(lblNumero);
this.Controls.Add(txtNumero);
this.Controls.Add(lblResultado);
}
private async Task<long> CalculoFibonacci(long numero)
{
long fibonacci = 1;
for (long i = 1; i < numero; ++i)
{
fibonacci *= i;
}
// Retardo arbitrario (demostración):
await Task.Delay (3100);
return fibonacci;
}
#endregion COMMON_CODE
#region CLIENT_CODE
///<summary>
/// Código cliente para administrar la ejecución de la aplicación.
///</summary>
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new FormularioConAsync());
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment