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++Builder Discussion :

Ex�cution d'un thread


Sujet :

C++Builder

  1. #1
    Membre confirm�
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Juin 2007
    Messages
    144
    D�tails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 144
    Par d�faut Ex�cution d'un thread
    Bonjour,

    J'ai test� le tutoriel sur les threads :
    http://chgi.developpez.com/thread

    Ca marche tr�s bien.

    Mon probl�me est : comment je peux int�gr� un thread dans mon application.

    Voici mon code :

    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
    //---------------------------------------------------------------------------
    
    void TGestionFrm::MyFunc(ushort u, String FenCaption)
    {
            TForm1 *Fen=NULL;
            Fen = new TForm1(this);
            Fen->Caption=FenCaption;
                                    .
                                    .
                                    .
            PEncours(1,"Calcul en cours, Patientez....");
            //Mon code sur le traitement d'une base de donn�es
    
            Fen->ShowModal();
            PEncours(2,"");}
    //---------------------------------------------------------------------------
    La fonction PEncours est appl�e plusieurs fois dans l'application.
    Elle affiche un controle : AdvCircularProgress qui est active (Enabled=true) si le 1er paramaitre est �gale � 1 sinon il le d�sactive (Enabled=false);

  2. #2
    Expert confirm�

    Avatar de pottiez
    Homme Profil pro
    D�veloppeur C++
    Inscrit en
    Novembre 2005
    Messages
    7 152
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 40
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activit� : D�veloppeur C++
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2005
    Messages : 7 152
    Par d�faut
    Ton thread est une classe, tu peut donc cr�er une instance de cette classe comme n'importe quelle classe et utiliser cette instance.

  3. #3
    Membre confirm�
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Juin 2007
    Messages
    144
    D�tails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 144
    Par d�faut
    Tout d'abord merci,

    Oui c'est vrai ce que tu dit, j'ai d�ja utiliser cette solution, mais mon prg se plante :

    La fenetre PatientezFrm est la fenetre qui permet d'afficher le controle AdvCircularProgress1 :

    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
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
     
    #include "PatientezUnit.h"
     
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma link "AdvProgressBar"
    #pragma link "AdvProgr"
    #pragma link "AdvCircularProgress"
    #pragma resource "*.dfm"
    TPatientezFrm *PatientezFrm;
    //ThreadClass1 *MyThread1;
    //---------------------------------------------------------------------------
    __fastcall TPatientezFrm::TPatientezFrm(TComponent* Owner)
            : TForm(Owner)
    {
     
    }
    //---------------------------------------------------------------------------
     
     
    void __fastcall TPatientezFrm::FormActivate(TObject *Sender)
    {
            //Synchronize(UpdateCaption);
            PatientezFrm->AdvCircularProgress1->Enabled=true;
            PatientezFrm->AdvCircularProgress1->Refresh();
     
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TPatientezFrm::FormClose(TObject *Sender,
          TCloseAction &Action)
    {
    /*        PatientezFrm->AdvCircularProgress1->Enabled=false;
            MyThread1 = new ThreadClass1(false);
            MyThread1->Suspend();*/
            if(MonThread) Action = caNone;
            PatientezFrm->AdvCircularProgress1->Enabled=false;
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TPatientezFrm::FormCreate(TObject *Sender)
    {
            MyThread1 = new ThreadClass1(true);
            MyThread1->Resume();
    }
    //---------------------------------------------------------------------------
     
    void __fastcall TPatientezFrm::FormPaint(TObject *Sender)
    {
            MonThread = new TMonThread(false);
            MonThread->OnTerminate = FinDeMonThread;
            EnableWindow(Handle, false);
     
    }
    //---------------------------------------------------------------------------
    void __fastcall  TPatientezFrm::FinDeMonThread(TObject *Sender)
    {
            EnableWindow(Handle, true);
            msg->Caption = "Thread terminé";
            delete MonThread;
            MonThread = NULL;
    }
     
    //---------------------------------------------------------------------------

  4. #4
    Membre �m�rite
    Homme Profil pro
    Consultant ERP
    Inscrit en
    F�vrier 2004
    Messages
    644
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activit� : Consultant ERP

    Informations forums :
    Inscription : F�vrier 2004
    Messages : 644
    Par d�faut
    Cr�e une classe h�ritant de TThread, tu surcharges le constructeur si n�cessaire et la m�thode Execute.
    Ensuite, il te suffit d'instancer ta classe et laisser ton thread s'amuser seul. Un join est n�cessaire en fin de vie de ton application.

  5. #5
    Membre confirm�
    Profil pro
    D�veloppeur informatique
    Inscrit en
    Juin 2007
    Messages
    144
    D�tails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activit� : D�veloppeur informatique
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 144
    Par d�faut
    Si tu peux m'expliquer par un bout de code.

    Merci

  6. #6
    Membre �m�rite
    Homme Profil pro
    Consultant ERP
    Inscrit en
    F�vrier 2004
    Messages
    644
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activit� : Consultant ERP

    Informations forums :
    Inscription : F�vrier 2004
    Messages : 644
    Par d�faut
    aller je fais un effort
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    class MyThread : public TThread {
    public:
      MyThread( bool est_ce_que_je_suspend_mon_thread = false ) : TThread( est_ce_que_je_suspend_mon_thread ) {
        // Je fais le configure si nécesssaire
      }
      virtual void Execute() {
         // Je fais quelque chose
      }
    private:
    };
    PS : Suffit de lire la doc de Borland :'(

    F1 -> TThread -> Voir utilisation TThread.

Discussions similaires

  1. l'ordre d’ex�cution d'un thread
    Par amazircool dans le forum Concurrence et multi-thread
    R�ponses: 9
    Dernier message: 30/12/2007, 17h39
  2. Ex�cution d'un Thread dans une m�thode statique
    Par barbiche dans le forum D�buter avec Java
    R�ponses: 3
    Dernier message: 03/05/2007, 14h25
  3. R�ponses: 6
    Dernier message: 20/03/2007, 22h23
  4. [Thread] Stopper l'ex�cution d'un thread brutalement
    Par Razgriz dans le forum Concurrence et multi-thread
    R�ponses: 7
    Dernier message: 31/01/2007, 19h55
  5. [java.util.Timer]Comment arr�ter l'ex�cution d'un Thread
    Par Invit� dans le forum Concurrence et multi-thread
    R�ponses: 1
    Dernier message: 07/06/2006, 07h54

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