task 调用函数问题 求助大神
DataTable d1, d2, d3;private void button3_Click(object sender, EventArgs e)
{
Task T1 = new Task(()=>getValue("1",d1));
Task T2 = new Task(() => getValue("2", d2));
Task T3 = new Task(() => getValue("3", d3));
T1.Start();
T2.Start();
T3.Start();
Task.WaitAll(T1,T2,T3);
MessageBox.Show("ok");
}
private void getValue(String V1,DataTable T)
{
T = new DataTable();
T.Columns.Add("A1");
if (V1.Equals("1"))
{
T.Rows.Add("1");
}
if (V1.Equals("2"))
{
T.Rows.Add("2");
}
if (V1.Equals("3"))
{
T.Rows.Add("3");
}
}
为什么最后d1,d2,d3全等于null ???????
尝试加上 out 修饰符试试?
private void button1_Click(object sender, EventArgs e)
{
var T1 = new Task(() => getValue("1", out d1));
var T2 = new Task(() => getValue("2", out d2));
var T3 = new Task(() => getValue("3", out d3));
T1.Start();
T2.Start();
T3.Start();
Task.WaitAll(T1, T2, T3);
MessageBox.Show("ok");
}
private void getValue(string V1, out DataTable T)
{
T = new DataTable();
T.Columns.Add("A1");
switch (V1)
{
case "1":
T.Rows.Add("1");
break;
case "2":
T.Rows.Add("2");
break;
case "3":
T.Rows.Add("3");
break;
}
}
页:
[1]