日期:2014-05-17 浏览次数:20646 次
public static string GetResult(string str)
{
//简化了逻辑
Thread.Sleep(2000);
return "传进来的文本是:" + str;
}
public static Dictionary<string, string> dic = new Dictionary<string, string>();
static void Main(string[] args)
{
//测试数据
dic.Add("AAAA","");
dic.Add("BBBB", "");
dic.Add("CCCC", "");
//遍历Dictionary,使用其key作为参数调用GetResult方法,并将返回值记录在对应的value中
List<string> keys = dic.Keys.ToList();
for (int i = 0; i < keys.Count; i++)
{
//请问这里如何使用多线程去调用呢?
dic[keys[i]] = GetResult(keys[i]);
}
//等待调用完成打印所有信息
for (int i = 0; i < keys.Count; i++)
{
Console.WriteLine(dic[keys[i]]);
}
}
Thread t = new Thread(new ThreadStart(()=>{GetResult(keys[i]}));
t.Start();