0
爲什麼導致錯誤結果的任務列表。 任何人都可以給方向?並行任務導致錯誤結果
這是我的代碼到目前爲止。
static string[] kk = new string[] { "a", "b", "c", "d", "e", "f" };
static void Main(string[] args)
{
//Parallel.For(0, 10, i => Test(i));
//Console.ReadLine();
foreach(string str in kk)
{
Task<string> task = Task.Factory.StartNew(() => Test(str));
listTask.Add(task);
}
do
{
Thread.Sleep(100);
}while(!Task.WaitAll(listTask.ToArray(),1000));
foreach (Task<string> task in listTask)
{
Console.WriteLine(task.Status.ToString() + " : " + task.Result);
}
Console.ReadLine();
}
private static string Test(string i)
{
return "test " + i;
}
我期望導致這樣的:
測試一個 試驗B 試驗C 測試d 測試電子郵件 測試f
但是我上面的代碼得到的是這樣的: 測試f test f test f test f test f test f
在此先感謝。
感謝@dcastro您的指導和解釋:) –