using System;
using System.Threading;
using System.Windows.Forms;
namespace ControlTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = Var.cars;
}
private void button1_Click(object sender, EventArgs e)
{
CarInfo carInfo = new CarInfo(textBox1.Text, int.Parse(textBox2.Text), int.Parse(textBox3.Text));
Var.cars.Add(carInfo);
}
private void button2_Click(object sender, EventArgs e)
{
ThreadWithState tws = new ThreadWithState(textBox1.Text, int.Parse(textBox2.Text), int.Parse(textBox3.Text));
tws.DataChangeCompleted += Tws_DataChangeCompleted;
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.IsBackground = true;
t.Start();
}
private void Tws_DataChangeCompleted()
{
dataGridView1.Invalidate();
}
}
public class ThreadWithState
{
public delegate void DataChangeCompletedEventHandler();
public event DataChangeCompletedEventHandler DataChangeCompleted;
private string carname;
int caryear = 0;
int carkm = 0;
// The constructor obtains the state information.
public ThreadWithState(string name, int year, int km)
{
carname = name;
caryear = year;
carkm = km;
}
public void ThreadProc()
{
Thread.Sleep(5000);
foreach (CarInfo carInfo in Var.cars)
{
if (carInfo.CarName == carname)
{
carInfo.Year = caryear;
carInfo.Km = carkm;
}
}
DataChangeCompleted();
}
}
}