views:

318

answers:

1

hi all;

i am using compact frame work for my windows mobile application in which i have pass more than one request to the server and receive response for each request in an array.

the problem is there when i should access these arrays because i m starting the threads in a for loop and the after completing the loop i have to access these arrays.

i m very much confused in, how will i know and that all threads have completed so that i start the processing on these arrays

help is require asap. please.

+2  A: 

How about this:

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;
    }
}
Bryan