class MultiThreading
{
public class ThreadClass
{
public string InputString { get; private set; }
public int StartPos { get; private set; }
public List<SearchAlgorithm.CandidateStr> completeModels;
public List<SearchAlgorithm.CandidateStr> partialModels;
public ThreadClass(string s, int sPos)
{
InputString = s;
StartPos = sPos;
completeModels = new List<SearchAlgorithm.CandidateStr>();
partialModels = new List<SearchAlgorithm.CandidateStr>();
}
public void Run(int strandID)
{
Thread t = new Thread(() => this._run(strandID));
t.Start();
}
private void _run(int strandID)
{
SearchAlgorithm.SearchInOneDirection(strandID, ref this.completeModels, ref this.partialModels);
}
public static void CombineResult(
List<ThreadClass> tc,
out List<SearchAlgorithm.CandidateStr> combinedCompleteModels,
out List<SearchAlgorithm.CandidateStr> combinedPartialModels)
{
// combine the result
}
}
}
class Program
{
static void Main(string s, int strandID)
{
int lenCutoff = 10000;
if (s.Length > lenCutoff)
{
var threads = new List<MultiThreading.ThreadClass>();
for (int i = 0; i <= s.Length; i += lenCutoff)
{
threads.Add(new MultiThreading.ThreadClass(s.Substring(i, lenCutoff), i));
threads[threads.Count - 1].Run(strandID);
}
**// How can I wait till all thread in threads list to finish?**
}
}
}
我的問題是我該如何等待「線程」列表中的所有線程完成,然後再調用CombineResult方法?多線程中的等待問題
感謝
如果您在使用C#4.0中,你有沒有使用[任務並行庫(HTTP考慮:// msdn.microsoft.com/en-us/library/dd460717.aspx)?非常喜歡直接編寫自己的線程。 – 2012-07-26 16:31:43
您可能最好閱讀[Task Class](http://msdn.microsoft.com/zh-cn/library/dd235678),它內置了'WaitAll'方法。 – nicholas 2012-07-26 16:31:59
可能重複[如何等待線程完成與.NET?](http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net) – 2012-07-26 16:33:42