0
我正在使用緊湊框架工作爲我的Windows移動應用程序,其中我已經傳遞多個請求到服務器和接收數組中的每個請求的響應。多線程在c#精簡框架
問題出在我應該訪問這些數組時,因爲我在for循環中啓動線程,完成循環後我必須訪問這些數組。
I M很迷糊中,我怎麼知道,所有線程都完成,使我對這些陣列
幫助需要儘快開始處理。請。
我正在使用緊湊框架工作爲我的Windows移動應用程序,其中我已經傳遞多個請求到服務器和接收數組中的每個請求的響應。多線程在c#精簡框架
問題出在我應該訪問這些數組時,因爲我在for循環中啓動線程,完成循環後我必須訪問這些數組。
I M很迷糊中,我怎麼知道,所有線程都完成,使我對這些陣列
幫助需要儘快開始處理。請。
如何:
private readonly object syncRoot = new object();
private object[] responses;
public void StartThreads(int numberOfThreads)
{
this.responses = new object[numberOfThreads];
List<Thread> threads = new List<Thread>();
for (int i = 0; i < numberOfThreads; ++i)
{
Thread thread = new Thread(this.DoWork);
thread.Name = i.ToString();
threads.Add(thread);
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
// all threads are done.
}
private void DoWork()
{
// do web call
// object response = web.GetResponse();
int threadNumber = Convert.ToInt32(Thread.CurrentThread.Name);
lock (this.syncRoot)
{
this.responses[threadNumber] = response;
}
}
//Declare this in class
public delegate void delege();
//Write this lines when you want to background thread start
Thread thread = new Thread(new ThreadStart(() => {
//Do what you what with backgorund threading , can not use any interface comand here
BeginInvoke(new delege(() => {
//Do here any main thread thread job,this can do interface and control jobs without any error
}));
}));
thread.Start();
爲什麼不直接使用一個線程發送請求,並對其進行處理從隊列? – 2009-10-10 04:36:27