2009-10-10 5 views
0

J'utilise le travail de cadre compact pour mon application Windows Mobile dans lequel j'ai passé plus d'une requête au serveur et recevoir une réponse pour chaque requête dans un tableau. Le problème est là quand je devrais accéder à ces tableaux car je commence les threads dans une boucle for et après avoir terminé la boucle, je dois accéder à ces tableaux.multithreading dans C# compact framework

je suis très confus dans, comment puis-je savoir et que toutes les discussions ont terminé pour que je commence le traitement sur ces tableaux

aide est besoin dès que possible. S'il vous plaît.

+0

Pourquoi ne pas simplement utiliser un seul thread pour envoyer les demandes et les traiter de la file d'attente? –

Répondre

2

Que diriez-vous ceci:

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; 
    } 
} 
0
//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(); 
Questions connexes