对于线程与委托的知识也不是了解的很多,仅限稍微的使用而已。也是百度去参考教程而已
1.实体类:
/// <summary>
/// 实体类
/// </summary>
public class TestModue
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private int old;
public int Old
{
get { return old; }
set { old = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private DateTime time;
public DateTime Time
{
get { return time; }
set { time = value; }
}
}
2.代码块:
/// <summary>
/// 功能操作类
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string He = "Thread_test";
TestModue t = new TestModue();
private void button1_Click(object sender, EventArgs e)
{
//无参线程
Thread t1 = new Thread(new ThreadStart(H1));
t1.Start();
//有参线程[单个参数]
Thread t2 = new Thread(new ParameterizedThreadStart(H2));
t2.Start(He);
//有参线程[实体]
Thread t3 = new Thread(new ParameterizedThreadStart(H3));
t3.Start(t);
}
/// <summary>
/// 无参线程执行MessageBox
/// </summary>
public void H1()
{
string hello = "Hello World";
MessageBox.Show(hello);
}
/// <summary>
/// 有参线程执行TextBox赋值操作[单个参数]
/// </summary>
/// <param name="He"></param>
public void H2(object He)
{
//委托代理
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { H2(He); }));
return;
}
string he = (string)He;
this.textBox1.Text = he;
}
/// <summary>
/// 有参线程执行DataGridView赋值操作[实体类]
/// </summary>
/// <param name="obj"></param>
private void H3(object t)
{
//委托代理
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { H3(t); }));
return;
}
TestModue tt = (TestModue)t;
List<TestModue> tlist = new List<TestModue>();
tlist.Add(tt);
this.dataGridView1.DataSource = tlist;
}
//为实体类赋值
private void Form1_Load(object sender, EventArgs e)
{
t.Id = 1;
t.Old = 22;
t.Sex = "男";
t.Time = DateTime.Now;
}
}
2.效果图: