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

 Delphi Discussion :

Thread Anti Flood


Sujet :

Delphi

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations forums :
    Inscription : Juin 2012
    Messages : 142
    Points : 80
    Points
    80
    Par défaut Thread Anti Flood
    Boujour,

    J'ai un petit problème de contrôle d'un thread

    Le but du thread est de faire une petite attente de 2 secondes entre chaque envoie de message
    Si le thread est en cours on envoie un message : "!!! Flooding !!!"

    Thread:

    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
    unit AntiFlood;
     
    interface
     uses SysUtils, Windows, Classes;
     
    type
      TFlood = class(TThread)
     
      protected
     
      procedure Execute; override;
     
    end;
     
    implementation
     
    procedure TFlood.Execute;
    begin
      while not Terminated do
       begin
         Sleep(2000);
     
         Terminate;
         FreeOnTerminate := False;
       end;
    end;
     
    end.
    Form :

    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
    procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin
     
     if (Flood <> nil) and (not Flood.CheckTerminated) and (Key = Vk_Return) then
      begin
        Listbox1.Items.BeginUpdate;
        Listbox1.Items.Add('!!! Flooding !!!');
        Listbox1.Items.EndUpdate;
        Exit;
      end;
     
     
     if (Key = Vk_Return) then
      begin
        Flood := TFlood.Create(True);
        Flood.FreeOnTerminate := True;
        Flood.Resume;
     
        Listbox1.Items.BeginUpdate;
        Listbox1.Items.Add(Edit1.Text);
        Listbox1.Items.EndUpdate;
      end
     
    end;
    Nom : Capture.PNG
Affichages : 174
Taille : 21,0 Ko

    Merci pour aide

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    399
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 399
    Points : 646
    Points
    646
    Par défaut
    tu ne peux utiliser FreeOnterminate que dans le create de ton thread, n'utilise pas resume

    dans ton utilisation un thread est franchement inutile, un banal GetTickCount suffit amplement , BeginUpdate / EndUpdate sont aussi inutiles vu que tu ne rajoutes qu'un seul élément



    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
    procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin
     
     if  (Key = Vk_Return) and (GetTickcount-FFloodTick<2000)  then
      begin
     
        Listbox1.Items.Add('!!! Flooding !!!');
     
        Exit;
      end;
     
     
     if (Key = Vk_Return) then
      begin
     
        FFLoodTick := GetTickCount;
     
     
        Listbox1.Items.Add(Edit1.Text);
     
      end
     
    end;

  3. #3
    Invité
    Invité(e)
    Par défaut
    Ta variable Floodest bien initialisée à nil ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Flood := nil; // par ex. dans le FormCreate

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    399
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 399
    Points : 646
    Points
    646
    Par défaut
    et si vraiment tu veux utiliser un thread (qui est totalement inutile ici )

    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
    type
     
     TFlood = class(TThread)
     
      constructor Create(ACreateSuspended : boolean;Event:TNotifyEvent);
      procedure Execute; override;
     
    end;
     
    constructor TFlood.Create(ACreateSuspended : boolean;Event:TNotifyEvent);
    begin
      FreeOnTerminate := true;
     
      OnTerminate := Event;
     
      inherited Create(ACreateSuspended);
    end;
     
    procedure TFlood.Execute;
    begin
      while not Terminated do
       begin
         Sleep(2000);
     
         Terminate;
     
       end;
    end;


    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
    procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin
     
     if (Key = Vk_Return) and assigned(FFLood) then
      begin
     
        ListBox1.items.Add('!!! Flooding !!!');
     
        Exit;
      end;
     
     
     if (Key = Vk_Return) then
      begin
     
        FFlood := TFlood.Create(false,doNoFlood);
     
        Listbox1.Items.Add(edit1.text);
      end
     
    end;
     
     
    procedure TFOrm6.doNoFlood(Sender: TObject);
    begin
      FFLood := nil;
    end;

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

Discussions similaires

  1. Système anti-flood pour formulaire
    Par Dsphinx dans le forum Langage
    Réponses: 15
    Dernier message: 29/12/2006, 08h36
  2. [Dates] Vérifier la logique d'un script anti-flood
    Par psychoBob dans le forum Langage
    Réponses: 1
    Dernier message: 23/04/2006, 14h47
  3. [Dates] Comparaison de date entre deux IP (anti-flood)
    Par psychoBob dans le forum Langage
    Réponses: 79
    Dernier message: 23/12/2005, 17h19

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