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 :

Pause en continuant


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Mars 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2015
    Messages : 34
    Points : 32
    Points
    32
    Par défaut Pause en continuant
    Bonjour,

    En bref mon code envoie une succession de ping et regarde à la volée les réponses.
    J'ai besoin de faire une pause dans mon programme pour arrêter l'envie des ping mais il doit continuer de lire les réponses.
    Du coup si j'utilise :
    Il arrête les deux. Comment faire pour stopper seulement les envois ?

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.NetworkInformation;
    using System.Runtime.InteropServices;
    using System.Threading;
     
    namespace MultiPing
    {
        class Program
        {
            static void Main(string[] args)
            {
                string Network = "10.240.0.0/15";
                //string motACouper = "/";
                //string[] chaineArrive = Network.Split(new string[] { motACouper }, StringSplitOptions.None);
                string[] chaineArrive = Network.Split('/');
                int masque = Convert.ToInt32(chaineArrive[1]);
                //string[] TabOctes = Network.Split(new string[] { chaineArrive[0] }, StringSplitOptions.None);
                string[] TabOctes = chaineArrive[0].Split('.');
                int Octet1 = Convert.ToInt32(TabOctes[0]);
                int Octet2 = Convert.ToInt32(TabOctes[1]);
                int Octet3 = Convert.ToInt32(TabOctes[2]);
                int Octet4 = Convert.ToInt32(TabOctes[3]);
                string Octet1Bin = Convert.ToString(Octet1, 2);
                Octet1Bin = Octet1Bin.PadLeft(8, '0');
                string Octet2Bin = Convert.ToString(Octet2, 2);
                Octet2Bin = Octet2Bin.PadLeft(8, '0');
                string Octet3Bin = Convert.ToString(Octet3, 2);
                Octet3Bin = Octet3Bin.PadLeft(8, '0');
                string Octet4Bin = Convert.ToString(Octet4, 2);
                Octet4Bin = Octet4Bin.PadLeft(8, '0');
                int Octet1Debut;
                int Octet1Fin;
                int Octet2Debut;
                int Octet2Fin;
                int Octet3Debut;
                int Octet3Fin;
                int Octet4Debut;
                int Octet4Fin;
                if(masque >24)       // modifie octect 4
                {
                    char[] Octet4BinGlobal = Octet4Bin.ToCharArray();
                    char[] Octect4BinBroadcast = Octet4Bin.ToCharArray();
                    int masqueOctet =8-( 32 - masque);
                    for(int a=0;a<8;a++)
                    {
                        if (a >( masqueOctet-1))
                        {
                            Octet4BinGlobal[a] = '0';
                            Octect4BinBroadcast[a] = '1';
                        }
                    }
     
                    Octet1Debut=Octet1;
                    Octet1Fin=(Octet1+1);
                    Octet2Debut=Octet2;
                    Octet2Fin=(Octet2+1);
                    Octet3Debut=Octet3;
                    Octet3Fin=(Octet3+1);
                    string Octet4Globalstr = new string(Octet4BinGlobal);
                    Octet4Debut = Convert.ToInt32(Octet4Globalstr, 2);
                    string Octet4Broadcaststr = new string(Octect4BinBroadcast);
                    Octet4Fin = (Convert.ToInt32(Octet4Broadcaststr, 2)+1);
                }
                else if(masque >16)  //modifie octect 3
                {
                    Console.WriteLine("je modifie l'octet 3");
                    char[] Octet3BinGlobal = Octet3Bin.ToCharArray();
                    char[] Octect3BinBroadcast = Octet3Bin.ToCharArray();
                    int masqueOctet = 8-(24 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet-1))
                        {
                            Octet3BinGlobal[a] = '0';
                            Octect3BinBroadcast[a] = '1';
                        }
                    }
                    Octet1Debut = Octet1;
                    Octet1Fin = (Octet1+1);
                    Octet2Debut = Octet2;
                    Octet2Fin = (Octet2+1);
                    string Octet3Globalstr = new string(Octet3BinGlobal);
                    Octet3Debut = Convert.ToInt32(Octet3Globalstr, 2);
                    string Octet3Broadcaststr = new string(Octet3BinGlobal);
                    Octet3Fin = (Convert.ToInt32(Octet3Broadcaststr, 2)+1); 
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                else if(masque >8)  // modifie octect 2
                {
                    char[] Octet2BinGlobal = Octet2Bin.ToCharArray();
                    char[] Octect2BinBroadcast = Octet2Bin.ToCharArray();
                    int masqueOctet = 8-(16 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet-1))
                        {
                            Octet2BinGlobal[a] = '0';
                            Octect2BinBroadcast[a] = '1';
                        }
                    }
     
                    int Octet3Global = 
                    Octet1Debut = Octet1;
                    Octet1Fin =( Octet1 +1);
                    string Octet2Globalstr = new string(Octet2BinGlobal);
                    Octet2Debut = Convert.ToInt32(Octet2Globalstr, 2);
                    string Octet2Broadcaststr = new string(Octect2BinBroadcast);
                    Octet2Fin =( Convert.ToInt32(Octet2Broadcaststr, 2)+1);
                    Octet3Debut = 0;
                    Octet3Fin = 256;
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                else               // modifie octect 1
                {
                    char[] Octet1BinGlobal = Octet1Bin.ToCharArray();
                    char[] Octect1BinBroadcast = Octet1Bin.ToCharArray();
                    int masqueOctet =8-( 8 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet-1))
                        {
                            Octet1BinGlobal[a] = '0';
                            Octect1BinBroadcast[a] = '1';
                        }
                    }
                    string Octet1Globalstr = new string(Octet1BinGlobal);
                    Octet1Debut = Convert.ToInt32(Octet1Globalstr, 2);
                    string Octet1Broadcaststr = new string(Octect1BinBroadcast);
                    Octet1Fin = (Convert.ToInt32(Octet1Broadcaststr, 2)+1);
                    Octet2Debut = 0;
                    Octet2Fin = 256;
                    Octet3Debut = 0;
                    Octet3Fin = 256;
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                SendMultiPing(Octet1Debut,Octet1Fin,Octet2Debut,Octet2Fin,Octet3Debut,Octet3Fin,Octet4Debut,Octet4Fin);
            }
     
            private static void SendMultiPing(int a,int ab,int b,int bb,int c,int cb,int d,int db)
            {
                int i = 0;
                string address;
                //int i = 0;
                for (int w = a; w < ab; w++)
                {
                    for (int x = b; x < bb; x++)
                    {
                        for (int y = c; y < cb; y++)
                        {
     
                            for (int z = d; z < db; z++)
                            {
                                address = "10." + Convert.ToString(x) + "." + Convert.ToString(y) + "." + Convert.ToString(z);
                                Ping pong = new Ping();
                                pong.PingCompleted += PingCompleted;
                                pong.SendAsync(address, new byte[32]);
                                i++;
                                if(i > 5000)
                                {
                                    Console.WriteLine("pause");
                                    i = 0;
                                    //for(int v=0;v<90000000;v++)
                                    //{
                                        //attend les reponse pour les suprimé et faire de la place
     
                                    //}
                                    Thread.Sleep(5000);
                                    Console.WriteLine("Fin Pause");
                                }
                            }
                        }
     
                    }
                }
            }
     
            private static void PingCompleted(object sender, PingCompletedEventArgs e)
            {
                if (e.Reply.Status == IPStatus.Success)
                {
                    Console.WriteLine(e.Reply.Address.ToString());
                    ((Ping)sender).PingCompleted -= PingCompleted;
                }
                if(e.Reply.Status==IPStatus.TimedOut)
                {
                    //kill the instance classe
                    ((Ping)sender).PingCompleted -= PingCompleted;
                }
     
            }
        }
    }
    Merci.

  2. #2
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2002
    Messages : 255
    Points : 445
    Points
    445
    Par défaut
    doublegold,

    C'est normal tu bloques le thread principal, faut en créer un autre par exemple (simplifié) :
    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
     
     int Octet1Debut=0;
     int Octet1Fin=0;
     
     int[] T = new int[] { Octet1Debut, Octet1Fin };
     
                new Thread(new ParameterizedThreadStart((o) =>
                {
                    //for (int w = T[0]; w < T[1]; w++) etc...
     
                    int i = 0;
                    for (; ; )
                    {
                        i++;
                        if (i > 5000)
                        {
                            i = 0;
                            Thread.Sleep(5000);
                        }
     
                    }
                })).Start(T);
    Mais faut avouer que le code pique un peu les yeux.

    Salutations.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Mars 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Loir et Cher (Centre)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2015
    Messages : 34
    Points : 32
    Points
    32
    Par défaut
    j'ai adapté le code selon ton exemples mais visiblement le thread ne ce lance pas , il saute le thread et passe directement à la fin
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.NetworkInformation;
    using System.Runtime.InteropServices;
    using System.Threading;
     
    namespace MultiPing
    {
        class Program
        {
            static void Main(string[] args)
            {
                string Network = "10.240.0.0/15";
                //string motACouper = "/";
                //string[] chaineArrive = Network.Split(new string[] { motACouper }, StringSplitOptions.None);
                string[] chaineArrive = Network.Split('/');
                int masque = Convert.ToInt32(chaineArrive[1]);
                //string[] TabOctes = Network.Split(new string[] { chaineArrive[0] }, StringSplitOptions.None);
                string[] TabOctes = chaineArrive[0].Split('.');
                int Octet1 = Convert.ToInt32(TabOctes[0]);
                int Octet2 = Convert.ToInt32(TabOctes[1]);
                int Octet3 = Convert.ToInt32(TabOctes[2]);
                int Octet4 = Convert.ToInt32(TabOctes[3]);
                string Octet1Bin = Convert.ToString(Octet1, 2);
                Octet1Bin = Octet1Bin.PadLeft(8, '0');
                string Octet2Bin = Convert.ToString(Octet2, 2);
                Octet2Bin = Octet2Bin.PadLeft(8, '0');
                string Octet3Bin = Convert.ToString(Octet3, 2);
                Octet3Bin = Octet3Bin.PadLeft(8, '0');
                string Octet4Bin = Convert.ToString(Octet4, 2);
                Octet4Bin = Octet4Bin.PadLeft(8, '0');
                int Octet1Debut;
                int Octet1Fin;
                int Octet2Debut;
                int Octet2Fin;
                int Octet3Debut;
                int Octet3Fin;
                int Octet4Debut;
                int Octet4Fin;
                if (masque > 24)       // modifie octect 4
                {
                    char[] Octet4BinGlobal = Octet4Bin.ToCharArray();
                    char[] Octect4BinBroadcast = Octet4Bin.ToCharArray();
                    int masqueOctet = 8 - (32 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet - 1))
                        {
                            Octet4BinGlobal[a] = '0';
                            Octect4BinBroadcast[a] = '1';
                        }
                    }
     
                    Octet1Debut = Octet1;
                    Octet1Fin = (Octet1 + 1);
                    Octet2Debut = Octet2;
                    Octet2Fin = (Octet2 + 1);
                    Octet3Debut = Octet3;
                    Octet3Fin = (Octet3 + 1);
                    string Octet4Globalstr = new string(Octet4BinGlobal);
                    Octet4Debut = Convert.ToInt32(Octet4Globalstr, 2);
                    string Octet4Broadcaststr = new string(Octect4BinBroadcast);
                    Octet4Fin = (Convert.ToInt32(Octet4Broadcaststr, 2) + 1);
                }
                else if (masque > 16)  //modifie octect 3
                {
                    Console.WriteLine("je modifie l'octet 3");
                    char[] Octet3BinGlobal = Octet3Bin.ToCharArray();
                    char[] Octect3BinBroadcast = Octet3Bin.ToCharArray();
                    int masqueOctet = 8 - (24 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet - 1))
                        {
                            Octet3BinGlobal[a] = '0';
                            Octect3BinBroadcast[a] = '1';
                        }
                    }
                    Octet1Debut = Octet1;
                    Octet1Fin = (Octet1 + 1);
                    Octet2Debut = Octet2;
                    Octet2Fin = (Octet2 + 1);
                    string Octet3Globalstr = new string(Octet3BinGlobal);
                    Octet3Debut = Convert.ToInt32(Octet3Globalstr, 2);
                    string Octet3Broadcaststr = new string(Octet3BinGlobal);
                    Octet3Fin = (Convert.ToInt32(Octet3Broadcaststr, 2) + 1);
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                else if (masque > 8)  // modifie octect 2
                {
                    char[] Octet2BinGlobal = Octet2Bin.ToCharArray();
                    char[] Octect2BinBroadcast = Octet2Bin.ToCharArray();
                    int masqueOctet = 8 - (16 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet - 1))
                        {
                            Octet2BinGlobal[a] = '0';
                            Octect2BinBroadcast[a] = '1';
                        }
                    }
     
                    int Octet3Global =
                    Octet1Debut = Octet1;
                    Octet1Fin = (Octet1 + 1);
                    string Octet2Globalstr = new string(Octet2BinGlobal);
                    Octet2Debut = Convert.ToInt32(Octet2Globalstr, 2);
                    string Octet2Broadcaststr = new string(Octect2BinBroadcast);
                    Octet2Fin = (Convert.ToInt32(Octet2Broadcaststr, 2) + 1);
                    Octet3Debut = 0;
                    Octet3Fin = 256;
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                else               // modifie octect 1
                {
                    char[] Octet1BinGlobal = Octet1Bin.ToCharArray();
                    char[] Octect1BinBroadcast = Octet1Bin.ToCharArray();
                    int masqueOctet = 8 - (8 - masque);
                    for (int a = 0; a < 8; a++)
                    {
                        if (a > (masqueOctet - 1))
                        {
                            Octet1BinGlobal[a] = '0';
                            Octect1BinBroadcast[a] = '1';
                        }
                    }
                    string Octet1Globalstr = new string(Octet1BinGlobal);
                    Octet1Debut = Convert.ToInt32(Octet1Globalstr, 2);
                    string Octet1Broadcaststr = new string(Octect1BinBroadcast);
                    Octet1Fin = (Convert.ToInt32(Octet1Broadcaststr, 2) + 1);
                    Octet2Debut = 0;
                    Octet2Fin = 256;
                    Octet3Debut = 0;
                    Octet3Fin = 256;
                    Octet4Debut = 0;
                    Octet4Fin = 256;
                }
                SendMultiPing(Octet1Debut, Octet1Fin, Octet2Debut, Octet2Fin, Octet3Debut, Octet3Fin, Octet4Debut, Octet4Fin);
            }
     
            private static void SendMultiPing(int a, int ab, int b, int bb, int c, int cb, int d, int db)
            {
                int[] T = new int[] { a, ab };
                new Thread(new ParameterizedThreadStart((o) =>
                {
                    int i = 0;
                    string address;
                    for (int w = T[0]; w < T[1]; w++)
                    {
                        for (int x = 0; x < 256; x++)
                        {
                            for (int y = 0; y < 256; y++)
                            {
     
                                for (int z = 0; z < 256; z++)
                                {
                                    address = "10." + Convert.ToString(x) + "." + Convert.ToString(y) + "." + Convert.ToString(z);
                                    Ping pong = new Ping();
                                    pong.PingCompleted += PingCompleted;
                                    pong.SendAsync(address, new byte[32]);
                                    i++;
                                    if (i > 10000)
                                    {
                                        //Console.WriteLine("pause");
                                        i = 0;
                                        //for(int v=0;v<90000000;v++)
                                        //{
                                        //attend les reponse pour les suprimé et faire de la place
     
                                        //}
                                        Thread.Sleep(7000);
                                        //Console.WriteLine("Fin Pause");
                                    }
                                }
                            }
     
                        }
                    }
                })).Start(T);        // fin du theard
                Console.WriteLine("finit");
                Console.Read();
            }
     
            private static void PingCompleted(object sender, PingCompletedEventArgs e)
            {
                if (e.Reply.Status == IPStatus.Success)
                {
                    Console.WriteLine(e.Reply.Address.ToString());
                    ((Ping)sender).PingCompleted -= PingCompleted;
                }
                if (e.Reply.Status == IPStatus.TimedOut)
                {
                    //kill the instance classe
                    ((Ping)sender).PingCompleted -= PingCompleted;
                }
     
            }
        }
    }

  4. #4
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    255
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2002
    Messages : 255
    Points : 445
    Points
    445
    Par défaut
    doublegold,

    Si tu rajoute ces lignes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     Console.WriteLine("entree Pause");
     Thread.Sleep(2000);
     Console.WriteLine("Fin Pause");
    ...tu constateras que le thread est bien exécuté. Il affiche "fini" car l'instruction est en dehors du thread.
    Ce n'est qu'une idée, il y a mille façons d'employer les threads, faut que tu recherches la bonne pour ton programme.
    Il y a des tutos sur developpez.

    Salutations.

Discussions similaires

  1. Réponses: 11
    Dernier message: 14/05/2014, 15h27
  2. Bouton continuer après pause dans le script
    Par tremeur53 dans le forum Langage
    Réponses: 12
    Dernier message: 05/11/2007, 16h54
  3. Réponses: 6
    Dernier message: 13/04/2007, 13h04
  4. [VB6] attendre un événement pour continuer l'exécution
    Par Argonz dans le forum VB 6 et antérieur
    Réponses: 21
    Dernier message: 12/11/2002, 13h08
  5. [langage] Continuer a parser une ligne
    Par D[r]eadLock dans le forum Langage
    Réponses: 5
    Dernier message: 30/09/2002, 18h49

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