[英]Control is not waiting till the thread finish its work
Below is my code , 下面是我的代码,
I know its not good coding practice , just creating a template to simulate thread
processing with parameter and return value 我知道它不是很好的编码实践,只是创建一个模板来模拟带有参数和返回值的
thread
处理
The problem it is reaching to last lines before invoked function finished , that causing exception
在调用的函数完成之前到达最后一行的问题,导致
exception
public class data_holder
{
public DataTable dtable = new DataTable();
public DataTable dt = new DataTable();
}
class Program
{
public static List<int> int_list1 = new List<int>();
public static List<int> int_list2 = new List<int>();
public static bool done = false;
static void Main(string[] args)
{
data_holder dh = new data_holder();
dh.dtable.Columns.Add("Id", typeof(string));
dh.dtable.Columns.Add("DOB", typeof(DateTime));
dh.dtable.Columns.Add("Refno", typeof(string));
for (int i = 0; i <= 10; i++)
{
DataRow dr = dh.dtable.NewRow();
dr["Id"] = i.ToString();
dr["DOB"] = DateTime.Now.AddDays(i);
dr["Refno"] = new Guid();
dh.dtable.Rows.Add(dr);
}
StringBuilder sb = new StringBuilder();
ThreadStart ts = new ThreadStart(processTable);
Thread t1 = new Thread(ts);
t1.Start();
t1.Join();
if (done == true)
//while(!done)
{
for (int i = 0; i < dh.dtable.Rows.Count; i++)
{
sb.Append(dh.dtable.Rows[i][0].ToString());
sb.Append(" ");
sb.Append(dh.dtable.Rows[i][1].ToString());
sb.Append(" ");
sb.Append(dh.dtable.Rows[i][2].ToString());
sb.Append(" ");
sb.Append(dh.dtable.Rows[i][3].ToString());
sb.Append(" ");
sb.Append(dh.dtable.Rows[i][4].ToString());
sb.Append(" ");
sb.Append(dh.dtable.Rows[i][5].ToString());
sb.Append(Environment.NewLine);
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
static void processTable()
{
data_holder dh = new data_holder();
dh.dtable.Columns.Add("col1", typeof(string));
dh.dtable.Columns.Add("col2", typeof(string));
dh.dtable.Columns.Add("col3", typeof(string));
for (int i = 0; i < dh.dtable.Rows.Count; i++)
{
dh.dtable.Rows[i]["col1"] = "col_1_Dummy";
dh.dtable.Rows[i]["col2"] = "col_2_Dummy";
dh.dtable.Rows[i]["col3"] = "col_3_Dummy";
}
done = true;
}}
It is showing error at 它在显示错误
sb.Append(dh.dtable.Rows[i][3].ToString());
Error :{"Cannot find column 3."}
错误:{“找不到列3。”}
I know its because datatable
is not yet ready , Should not execution control wait till call to processTable()
finished ? 我知道这是因为
datatable
尚未准备好,执行控制不应该等到对processTable()
调用完成吗?
processTable
works with local dh
variable and do nothing with global dh
variable processTable
与本地dh
变量一起使用,而与全局dh
变量无关
So Rows[i][3]
is referring to the forth column of [i]
-s row, which doesn't exist. 因此,
Rows[i][3]
指的是[i]
-s行的第四列,该列不存在。
That's what exception is saying about. 这就是例外。
Here is the solution 这是解决方案
credit @vyrp 信用@vyrp
There were two instance of data_holder and that was creating the problem declared it as 有两个data_holder实例,并且正在创建问题,声明为
public static data_holder dh = new data_holder();
and it works as expectation. 并按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.