El código de la aplicación es el siguiente:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace winapp1
{
public partial class Form1 : Form
{
private void calculaPares(object nombre)
{
int cont = 0;
System.Console.WriteLine("iniciando: {0}", ((mensaje)nombre).msg);
for (int i = 1; i <= 500000; cont += (i % 2 == 0 ? 1 : 0), i += 1) ;
((mensaje)nombre).msg= "pares " + cont.ToString();
}
public Form1()
{
InitializeComponent();
}
private void btn_iniciar_Click(object sender, EventArgs e)
{
mensaje objMsg1 = new mensaje("pares"); //objeto intercambio de datos creado
Thread t1 = new Thread(this.calculaPares); //Objeto hilo creado
this.lb_pares.Text = objMsg1.msg; //etiqueta de la forma = "pares"
t1.Start( objMsg1 ); //inicia el hilo
while (t1.ThreadState == ThreadState.Running) ; //ciclo para esperar al hilo
this.lb_pares.Text = objMsg1.msg; //etiqueta de la forma = resultado hilo
}
}
public class mensaje
{
String msg_;
public mensaje(String valor)
{
msg_ = valor;
}
public String msg
{
get
{
return msg_;
}
set
{
msg_ = value;
}
}
}
}
No hay comentarios:
Publicar un comentario
Are you ready?