Visualisation des thread graphique

Contenu du snippet

petit programme sur les Thread. Fait de Façon graphique.

Source / Exemple :


//main.cpp

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "main.h"
#include "thread1.h"
#include "thread2.h"
#include "thread3.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TMonThread *lpMonThread;
TMonThread2 *lpMonThread2;
TMonThread3 *lpMonThread3;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}

//---------------------------------------------------------------------------
//bouton fermeture du programme
//---------------------------------------------------------------------------

void __fastcall TForm1::FermetureProgClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DemarrerThread1Click(TObject *Sender)
{
lpMonThread = new TMonThread (false);
Form1->DemarrerThread1->Enabled=false;
Form1->FermerThread1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DemarrerThread2Click(TObject *Sender)
{
lpMonThread2 = new TMonThread2 (false);
Form1->DemarrerThread2->Enabled=false;
Form1->FermerThread2->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DemarrerThread3Click(TObject *Sender)
{
lpMonThread3 = new TMonThread3 (false);
Form1->DemarrerThread3->Enabled=false;
Form1->FermerThread3->Enabled=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FermerThread1Click(TObject *Sender)
{
lpMonThread -> Terminate();
Form1->DemarrerThread1->Enabled=true;
Form1->FermerThread1->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FermerThread2Click(TObject *Sender)
{
lpMonThread2 ->Terminate();
Form1->DemarrerThread2->Enabled=true;
Form1->FermerThread2->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FermerThread3Click(TObject *Sender)
{
lpMonThread3 ->Terminate();
Form1->DemarrerThread3->Enabled=true;
Form1->FermerThread3->Enabled=false;
}
//---------------------------------------------------------------------------

/***************************************************************************/
/*Priorité du thread 1
/***************************************************************************/
void __fastcall TForm1::TpLowestThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpLowest;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpHigherThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpHigher;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpIdleThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpIdle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpLowerThread1Click(TObject *Sender)
{
lpMonThread->Priority =  tpLower;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpNormalThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpNormal;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpHighestThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpHighest;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpTimeCriticalThread1Click(TObject *Sender)
{
lpMonThread->Priority = tpTimeCritical;
}
//---------------------------------------------------------------------------

//***************************************************************************
//Priorité du thread 2
//***************************************************************************

void __fastcall TForm1::TpNormalThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpNormal;
;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpHigherThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpHigher;        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpHighestThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpHighest;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpTimeCriticalThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpTimeCritical;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpLowerThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpLower;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpIdleThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpIdle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TpLowestThread2Click(TObject *Sender)
{
lpMonThread2->Priority = tpLowest;
}
//---------------------------------------------------------------------------

//changement de la couleur de fond
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
Form1->Color = clBlue;
Panel1->Color = clGray;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton2Click(TObject *Sender)
{
Form1->Color = clGreen;
Panel1->Color = clWhite;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//priorité du Troisiéme thread
//---------------------------------------------------------------------------
void __fastcall TForm1::TpIdleThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpIdle;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpLowestThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpLowest;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpLowerThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpLower;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpNormalThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpNormal;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpHigherThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpHigher;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpHighestThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpHighest;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TpTimeCriticalThread3Click(TObject *Sender)
{
lpMonThread3->Priority = tpTimeCritical;
}
//---------------------------------------------------------------------------

//main.h

#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// Composants gérés par l'EDI
        TBitBtn *FermetureProg;
        TButton *DemarrerThread1;
        TButton *DemarrerThread2;
        TButton *FermerThread1;
        TButton *FermerThread2;
        TButton *Brique;
        TPanel *Panel1;
        TButton *Brique2;
        TButton *TpIdleThread1;
        TButton *TpLowestThread1;
        TButton *TpLowerThread1;
        TButton *TpHighestThread1;
        TButton *TpHigherThread1;
        TButton *TpNormalThread1;
        TButton *TpHigherThread2;
        TButton *TpNormalThread2;
        TButton *TpLowerThread2;
        TButton *TpLowestThread2;
        TButton *TpIdleThread2;
        TLabel *Label40;
        TLabel *Label41;
        TLabel *Label43;
        TLabel *Label5;
        TLabel *Label6;
        TButton *TpTimeCriticalThread1;
        TLabel *Label1;
        TButton *TpHighestThread2;
        TButton *TpTimeCriticalThread2;
        TLabel *Label2;
        TLabel *Label3;
        TLabel *Label7;
        TLabel *Label4;
        TLabel *Label8;
        TLabel *Label9;
        TLabel *Label10;
        TLabel *Label11;
        TImage *Photo;
        TLabel *Label12;
        TLabel *Label13;
        TLabel *Label14;
        TLabel *Label15;
        TRadioButton *RadioButton1;
        TButton *Brique3;
        TButton *DemarrerThread3;
        TLabel *Label16;
        TLabel *Label17;
        TLabel *Label18;
        TLabel *Label19;
        TLabel *Label20;
        TLabel *Label21;
        TLabel *Label22;
        TLabel *Label23;
        TLabel *Label24;
        TButton *FermerThread3;
        TButton *TpIdleThread3;
        TButton *TpLowestThread3;
        TButton *TpLowerThread3;
        TButton *TpNormalThread3;
        TButton *TpHigherThread3;
        TButton *TpHighestThread3;
        TButton *TpTimeCriticalThread3;
        TRadioButton *RadioButton2;
        void __fastcall FermetureProgClick(TObject *Sender);
        void __fastcall DemarrerThread1Click(TObject *Sender);
        void __fastcall DemarrerThread2Click(TObject *Sender);
        void __fastcall FermerThread1Click(TObject *Sender);
        void __fastcall FermerThread2Click(TObject *Sender);
        void __fastcall TpLowestThread2Click(TObject *Sender);
        void __fastcall TpLowestThread1Click(TObject *Sender);
        void __fastcall TpHigherThread1Click(TObject *Sender);
        void __fastcall TpIdleThread2Click(TObject *Sender);
        void __fastcall TpIdleThread1Click(TObject *Sender);
        void __fastcall TpLowerThread1Click(TObject *Sender);
        void __fastcall TpNormalThread1Click(TObject *Sender);
        void __fastcall TpHighestThread1Click(TObject *Sender);
        void __fastcall TpTimeCriticalThread1Click(TObject *Sender);
        void __fastcall TpLowerThread2Click(TObject *Sender);
        void __fastcall TpNormalThread2Click(TObject *Sender);
        void __fastcall TpHigherThread2Click(TObject *Sender);
        void __fastcall TpHighestThread2Click(TObject *Sender);
        void __fastcall TpTimeCriticalThread2Click(TObject *Sender);
        void __fastcall RadioButton1Click(TObject *Sender);
        void __fastcall DemarrerThread3Click(TObject *Sender);
        void __fastcall FermerThread3Click(TObject *Sender);
        void __fastcall TpIdleThread3Click(TObject *Sender);
        void __fastcall TpLowestThread3Click(TObject *Sender);
        void __fastcall TpLowerThread3Click(TObject *Sender);
        void __fastcall TpNormalThread3Click(TObject *Sender);
        void __fastcall TpHigherThread3Click(TObject *Sender);
        void __fastcall TpHighestThread3Click(TObject *Sender);
        void __fastcall TpTimeCriticalThread3Click(TObject *Sender);
        void __fastcall RadioButton2Click(TObject *Sender);
private:	// Déclarations utilisateur
public:		// Déclarations utilisateur
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//thread.h
//--------------------------
#ifndef Thread1H
#define Thread1H
//-------------------------
#include <Classes.hpp>
//-------------------------
class TMonThread : public TThread
{
private:
        int X,Y,W,H,Xmax,Ymax;
        bool droite,bas;
protected:
        void __fastcall Execute();
        void __fastcall dessinner();
public:
        __fastcall TMonThread(bool CreateSuspended);
};
//-------------------------
#endif

//thread.cpp
//-------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "thread1.h"
#include "main.h"
#pragma package(smart_init)
//-------------------------------------
__fastcall TMonThread::TMonThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
Priority=tpIdle;
}

//-------------------------------------
void __fastcall TMonThread :: Execute()
{
X=Form1->Brique->Left;
Y=Form1->Brique->Top;
W=Form1->Brique->Width;
H=Form1->Brique->Height;
Xmax=Form1->Panel1->Width - Form1->Brique->Width;
Ymax=Form1->Panel1->Height - Form1->Brique->Height;

droite=true;
bas=true;

while(!Terminated)
        {
                if(droite)
                        {
                        X++;
                        }
                else
                        {
                        X--;
                        }
                if(X>=Xmax)
                        {
                        droite=false;
                        }
                if(X<=0)
                        {
                        droite=true;
                        }
                if(bas)
                        {
                        Y++;
                        }
                else
                        {
                        Y--;
                        }
                if(Y>=Ymax)
                        {
                        bas=false;
                        }
                if (Y<=0)
                        {
                        bas=true;
                        }
        Synchronize (dessinner);
        }
}

void __fastcall TMonThread :: dessinner (void)
{
        Form1->Brique->SetBounds(X,Y,W,H);
        Application->ProcessMessages();
}

//thread2.h
//--------------------------
#ifndef Thread2H
#define Thread2H
//-------------------------
#include <Classes.hpp>
//-------------------------
class TMonThread2 : public TThread
{
private:
        int X2,Y2,W2,H2,Xmax,Ymax;
        bool droite,bas;
protected:
        void __fastcall Execute();
        void __fastcall dessinner();
public:
        __fastcall TMonThread2(bool CreateSuspended);
};
//-------------------------
#endif

//thread2.cpp
//-------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "thread2.h"
#include "main.h"
#pragma package(smart_init)
//-------------------------------------
__fastcall TMonThread2::TMonThread2(bool CreateSuspended)
        : TThread(CreateSuspended)
{
Priority=tpIdle;
}

//-------------------------------------
void __fastcall TMonThread2 :: Execute()
{
X2=Form1->Brique2->Left;
Y2=Form1->Brique2->Top;
W2=Form1->Brique2->Width;
H2=Form1->Brique2->Height;
Xmax=Form1->Panel1->Width - Form1->Brique2->Width;
Ymax=Form1->Panel1->Height - Form1->Brique2->Height;

droite=true;
bas=true;

while(!Terminated)
        {
                if(droite)
                        {
                        X2++;
                        }
                else
                        {
                        X2--;
                        }
                if(X2>=Xmax)
                        {
                        droite=false;
                        }
                if(X2<=0)
                        {
                        droite=true;
                        }
                if(bas)
                        {
                        Y2++;
                        }
                else
                        {
                        Y2--;
                        }
                if(Y2>=Ymax)
                        {
                        bas=false;
                        }
                if (Y2<=0)
                        {
                        bas=true;
                        }
        Synchronize (dessinner);
        }
}

void __fastcall TMonThread2 :: dessinner (void)
{
        Form1->Brique2->SetBounds(X2,Y2,W2,H2);
        Application->ProcessMessages();
}

//thread3.h
#include <Classes.hpp>
//-------------------------
class TMonThread3 : public TThread
{
private:
        int X3,Y3,W3,H3,Xmax,Ymax;
        bool droite,bas;
protected:
        void __fastcall Execute();
        void __fastcall dessinner();
public:
        __fastcall TMonThread3(bool CreateSuspended);
};
//-------------------------
#endif

//thread3.cpp
//-------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "thread3.h"
#include "main.h"
#pragma package(smart_init)
//-------------------------------------
__fastcall TMonThread3::TMonThread3(bool CreateSuspended)
        : TThread(CreateSuspended)
{
Priority=tpIdle;
}

//-------------------------------------
void __fastcall TMonThread3 :: Execute()
{
X3=Form1->Brique3->Left;
Y3=Form1->Brique3->Top;
W3=Form1->Brique3->Width;
H3=Form1->Brique3->Height;
Xmax=Form1->Panel1->Width - Form1->Brique3->Width;
Ymax=Form1->Panel1->Height - Form1->Brique3->Height;

droite=true;
bas=true;

while(!Terminated)
        {
                if(droite)
                        {
                        X3++;
                        }
                else
                        {
                        X3--;
                        }
                if(X3>=Xmax)
                        {
                        droite=false;
                        }
                if(X3<=0)
                        {
                        droite=true;
                        }
                if(bas)
                        {
                        Y3++;
                        }
                else
                        {
                        Y3--;
                        }
                if(Y3>=Ymax)
                        {
                        bas=false;
                        }
                if (Y3<=0)
                        {
                        bas=true;
                        }
        Synchronize (dessinner);
        }
}

void __fastcall TMonThread3 :: dessinner (void)
{
        Form1->Brique3->SetBounds(X3,Y3,W3,H3);
        Application->ProcessMessages();
}

//voilà pour chaque thread c'est toujours la meme methode n'ésiter pas à demander si vous avez un probleme pour le faire tourner.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.