C#子线程让主窗体执行代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public delegate void OutDelegate(ParseXmlResult xmlResult);
public void insertDataGridView(ParseXmlResult xmlResult)
{
	if (dataGridViewRecord.InvokeRequired)
	{
		OutDelegate outdelegate = new OutDelegate(insertDataGridView);
		this.BeginInvoke(outdelegate, new object[] { xmlResult });
		return;
	}
 
	DateTime date;
	string[] format = { "yyyyMMddHHmmss" };
	DateTime.TryParseExact(xmlResult.time.Substring(0, 14),
		format,
		System.Globalization.CultureInfo.InvariantCulture,
		System.Globalization.DateTimeStyles.None,
		out date);
 
	dataGridViewRecord.Rows.Add(new object[] { dataGridViewRecord.Rows.Count + 1, date.ToString("yyyy-MM-dd HH:mm:ss"), xmlResult.plate, xmlResult.files[0] });
}
private Thread workThread;
private void ProcessWork()
{
	insertDataGridView(xmlResult);
}
private static void ProcessThread(object param)
{
	MainForm pThis = param as MainForm;
	pThis.ProcessWork();
}
private void btnSearch_Click(object sender, EventArgs e)
{
	workThread = new Thread(new ParameterizedThreadStart(ProcessThread));
	workThread.Start(this);
}

Leave a Comment