我已經試過這...C#等到所有的線程執行完畢
public ArrayList GetAllObjectAttributes()
{
List<Task> taskList = new List<Task>();
ArrayList allObjectAttributes = new ArrayList();
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder));}));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)); }));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)); }));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)); }));
Task.WaitAll(taskList.ToArray());
return allObjectAttributes;
}
這...
public ArrayList GetAllObjectAttributes()
{
Thread[] threads = new Thread[4];
ArrayList allObjectAttributes = new ArrayList();
threads[0] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
threads[1] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
threads[2] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
threads[3] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));
foreach(Thread thread in threads)
{
thread.Start();
thread.Join();
}
return allObjectAttributes;
}
這也太...
public ArrayList GetAllObjectAttributes()
{
Thread[] threads = new Thread[4];
ArrayList allObjectAttributes = new ArrayList();
threads[0] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
threads[1] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
threads[2] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
threads[3] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));
foreach(Thread thread in threads)
{
thread.Start();
}
while(threads[0].IsAlive || threads[1].IsAlive || threads[2].IsAlive || threads[3].IsAlive)
{
Thread.Sleep(500);
}
return allObjectAttributes;
}
我也試過 Spawn Multiple Threads for work then wait until all finished
我仍然在allObjectAttributes中的一個arraylist項目中獲得null。
然而,當我做
public ArrayList GetAllObjectAttributes()
{
ArrayList allObjectAttributes = new ArrayList();
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));
return allObjectAttributes;
}
我從來沒有在ArrayList的項目得到一個空項。
- 我在做什麼錯等到所有線程完成?
- 任何其他建議,以便所有4個線程完成執行後僅返回arraylist。
private List GetObjectAttributes(TreeViewAttrs tv) { List objectAttributes = new List(); string command = "COMMAND_TO_EXECUTE"; if (command != "") { List results = RunCommand(command); if (results == null) { return null; } if (results.Count > 0) { foreach (string result in results) { if (!result.Contains("" + tv + "")) { string[] res = reformatResponseString(result); //reformat the strings as per custom structure if (res != null) { objectAttributes.Add(res); } } } return objectAttributes; } } return null; }
從多線程中改變一個共享的,非線程安全的實例(在你的情況下是'ArrayList')是一個非常糟糕的主意。只需使用'ConcurrentBag',或者如果你使用.NET 4.5,則使用'Task.WhenAll',它實際上會產生一系列任務結果。 –
任何人都可以幫助格式化我的問題的最後一段嗎?我試着用4個空格格式化它(正如我在早上做的那樣),但它不起作用。好像我今天錯過了很多東西。大聲笑 –
你*肯定*有空傳播的地方的問題。最有可能的是一個種族或一個'ThreadStatic'或'ThreadLocal'變量隱藏在調用樹的更深處。雖然我發現'GetObjectAttributes'的發佈定義沒有任何錯誤 - 該函數看起來很純粹,所以我會查看'RunCommand'和'reformatResponseString' - 但這是一個練習。只要在你的執行鏈上的'if(something == null)'創建一些條件斷點並在調試器下運行 - 遲早你會發現你的罪魁禍首。 –