IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Boucle sur thread?


Sujet :

C#

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2012
    Messages
    233
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2012
    Messages : 233
    Points : 92
    Points
    92
    Par défaut Boucle sur thread?
    Bonjour,

    Dans la classe suivante:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    static void Main(string[] args)
            {
     
                ModelAccessor monModel = new ModelAccessor();
     
                 Thread th = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th1 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th2= new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th3 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th4 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th5 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th6 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th7 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th8 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
                 Thread th9 = new Thread(new ParameterizedThreadStart(monModel.CalculateValueClient));
     
     
     
                for (int i = 0; i < 1000; i+=10)
                {
                    th.Start(i);
                    th1.Start(i+1);
                    th2.Start(i+2);
                    th3.Start(i+3);
                    th4.Start(i+4);
                    th5.Start(i+5);
                    th6.Start(i+6);
                    th7.Start(i+7);
                    th8.Start(i+8);
                    th9.Start(i+9);
    Je cherche à réaliser une boucle sur des threads afin de lancer la méthode "CalculateValueClient" 1000 fois au total mais pas plus de 10 fois en même temps et de faire en sorte que, par exemple, dès que le thread qui s'occupe de i ait fini, alors, celui qui s'occupe de i+ se lance!

    Le bout de code ci-dessus ne réalise pas cette dernière condition...

    Quelqu'un peut me filer un coup de main?

    Merci d'avance!

  2. #2
    Inactif  
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Janvier 2007
    Messages
    6 604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet NTIC

    Informations forums :
    Inscription : Janvier 2007
    Messages : 6 604
    Points : 13 314
    Points
    13 314
    Par défaut
    Bonjour

    Aller voir du coté des objets de synchronisation : WaitHandle, ManualResetEvent, etc ....

    Je ne réponds pas aux questions techniques par MP ! Le forum est là pour ça...


    Une réponse vous a aidé ? utiliser le bouton

    "L’ennui dans ce monde, c’est que les idiots sont sûrs d’eux et les gens sensés pleins de doutes". B. Russel

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Janvier 2012
    Messages
    233
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2012
    Messages : 233
    Points : 92
    Points
    92
    Par défaut
    Merci! Ca a l'air de compiler comme ça! Juste pour info si qq'1 cherche un exemple!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    static void Main(string[] args)
            {
                const int ValueClientCalculations = 1000; 
     
                // One event is used for each ModelAccessor object
                ManualResetEvent[] doneEvents = new ManualResetEvent[ValueClientCalculations];
                ModelAccessor[] modelAccessorArray = new ModelAccessor[ValueClientCalculations];
     
                // Configure and launch threads using ThreadPool:
                Console.WriteLine("launching {0} tasks...", ValueClientCalculations);
                for (int i = 0; i < ValueClientCalculations; i++)
                {
                    doneEvents[i] = new ManualResetEvent(false);
                    ModelAccessor f = new ModelAccessor(i, doneEvents[i]);
                    modelAccessorArray[i] = f;
                    ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
                }
     
                // Wait for all threads in pool to calculation...
                WaitHandle.WaitAll(doneEvents);
                Console.WriteLine("All calculations are complete.");
     
                // Display the results...
                for (int i = 0; i < ValueClientCalculations; i++)
                {
                    ModelAccessor f = modelAccessorArray[i];
                    Console.WriteLine("ValueClient({0}) = {1}", f.N, f.valfin);
                }
            }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    public class ModelAccessor
    	{
            //Properties
            public List<Client> Clients { get; set; }
     
            public BankContext context = BankFactory.createContext();
            public ManualResetEvent doneEvent { get; set; }
            public int N {get {return _n;}}
            public int _n { get; set; }
            public decimal value { get; set; }
            public decimal valfin { get { return _valfin; } }
            public decimal _valfin { get; set; }
     
            //constructor
            public ModelAccessor(int n, ManualResetEvent doneEvent)
            {
                _n = n; 
                this.doneEvent = doneEvent; 
            }
     
            //Méthods
            public Client GetClient(int index)
            {
                Client client = context.Clients[index];
                return client; 
            }
     
            public List<Client> GetAllClient()
            {
                for (int i = 0; i < context.Clients.Count; i++)
                {
                    Client client = GetClient(i);
                    Clients.Add(client);
                }
                return Clients;
            }
     
            public int GetNumberClient()
            {
                return context.Clients.Count();
            }
     
            public decimal CalculateValueClient(int i)
            {
                Client client = GetClient(i);
                List<Account> comptesClient = new List<Account>();
     
                comptesClient.Concat(context.Dailys.Where(daily => daily.client == client)).Concat(context.Savings.Where(savings => savings.client == client))
                    .Concat(context.Youths.Where(youth => youth.client == client))
                    .Concat(context.Webs.Where(webs => webs.client == client));
     
                return value =  comptesClient.Sum(compte => compte.balance);
     
                Thread.Sleep(500);
                Console.WriteLine("La valeur financière du client est de " + value + "€.");        
            }
     
            // Wrapper method for use with thread pool.
            public void ThreadPoolCallback(Object threadContext)
            {
                int threadIndex = (int)threadContext;
                Console.WriteLine("thread {0} started...", threadIndex);
                _valfin = CalculateValueClient(_n);
                Console.WriteLine("thread {0} result calculated...", threadIndex);
                doneEvent.Set();
            }
    	}

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XSLT] Faire une boucle sur une variable [i]
    Par PoT_de_NuTeLLa dans le forum XSL/XSLT/XPATH
    Réponses: 8
    Dernier message: 07/06/2010, 12h45
  2. Macro sur Excel/Boucle sur les lettres
    Par life is magic dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 25/11/2005, 11h56
  3. [JDBC]Boucle sur tous les éléments du ResultSet
    Par Terminator dans le forum JDBC
    Réponses: 1
    Dernier message: 22/09/2005, 19h30
  4. L'installation d'XP boucle sur elle-même
    Par pf106 dans le forum Windows XP
    Réponses: 13
    Dernier message: 20/08/2005, 14h55
  5. [MFC] Boucle sur un bouton
    Par karl3i dans le forum MFC
    Réponses: 6
    Dernier message: 17/02/2004, 11h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo