Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 17, 2014 17:41
Show Gist options
  • Save Fhernd/59df3fcb56defb5dac5b to your computer and use it in GitHub Desktop.
Save Fhernd/59df3fcb56defb5dac5b to your computer and use it in GitHub Desktop.
Creación de threads foreground y background en una aplicación WinForms.
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Recetas.Multithreading.Cap01.R0107
{
public class ForegroundVsBackgroundWinForms : Form
{
private System.ComponentModel.IContainer components = null;
public ForegroundVsBackgroundWinForms()
{
InitializeComponents();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose (disposing);
}
protected void InitializeComponents()
{
this.SuspendLayout();
this.Font = new Font ("Trebuchet MS", 9);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmForegroundVsBackground";
this.Size = new Size (650, 231);
this.Text = "Foreground Vs Background Thread";
CrearBotones();
}
private void CrearBotones()
{
Button btnCrearBackgroundThread = new Button();
btnCrearBackgroundThread.Click += btnCrearBackgroundThread_Click;
btnCrearBackgroundThread.Location = new Point (17, 17);
btnCrearBackgroundThread.Name = "btnCrearBackgroundThread";
btnCrearBackgroundThread.Size = new Size (153, 23);
btnCrearBackgroundThread.Text = "Crear Thread Background";
this.Controls.Add (btnCrearBackgroundThread);
Button btnCrearForegroundThread = new Button ();
btnCrearForegroundThread.AutoSize = true;
btnCrearForegroundThread.Click += btnCrearForegroundThread_Click;
btnCrearForegroundThread.Location = new Point (201, 17);
btnCrearForegroundThread.Name = "btnCrearForegroundThread";
btnCrearForegroundThread.Size = new Size (153,23);
btnCrearForegroundThread.Text = "Crear Thread Foreground";
this.Controls.Add (btnCrearForegroundThread);
Label lblInstrucciones = new Label ();
lblInstrucciones.AutoSize = true;
lblInstrucciones.Location = new Point (17, 103);
lblInstrucciones.Text = String.Format ("Pasos:\n{0}\n{1}\n{2}",
"1. Inicie el Administrador de Tareas y seleccione el proceso ForegroundVsBackgroundWinForms.exe \n desde la pestaña Procesos.",
"2. Prueba primero con uno de los botones y cierre la aplicación.",
"3. Observe lo qué sucede después de cada clic en el Administrador de Tareas."
);
this.Controls.Add (lblInstrucciones);
}
public void btnCrearBackgroundThread_Click (object sender, EventArgs e)
{
// Crea un thread para ejecutarlo en segundo plano:
Thread t = new Thread (Retraso);
t.IsBackground = true;
t.Start();
MessageBox.Show ("Se ha invocado un thread que tomará 13 segundos en completarse.\n" +
"Cierre la aplicación y observe lo que sucede en el Administrador de Tareas");
}
public void btnCrearForegroundThread_Click(object sender, EventArgs e)
{
// Crea un thread para ejecución en primer plano:
Thread t = new Thread (Retraso);
t.IsBackground = false;
t.Start();
MessageBox.Show ("Se ha invocado un thread que tomará 13 segundos en completarse.\n" +
"Cierre la aplicación y observe lo que sucede en el Administrador de Tareas");
}
private void Retraso()
{
Thread.Sleep (TimeSpan.FromSeconds (10));
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run (new ForegroundVsBackgroundWinForms());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment