簡體   English   中英

C#緊湊框架中的多線程

[英]multithreading in c# compact framework

我正在為Windows Mobile應用程序使用緊湊型框架,在該應用程序中我已將多個請求傳遞給服務器,並收到數組中每個請求的響應。

問題是當我應該訪問這些數組時,因為我在for循環中啟動了線程,而在完成循環之后,我必須訪問這些數組。

我非常困惑,我怎么知道並且所有線程都已經完成,所以我開始對這些數組進行處理

需要盡快提供幫助。 請。

這個怎么樣:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM