Thread et Label

Résolu
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 - 25 janv. 2005 à 13:35
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 27 janv. 2005 à 18:06
Bonjour tout le monde,


voila je débute en C# et j'ai donc quelques problemes... en fait des le lancement de mon programme, je lance un thread qui a pour but de modifier de maniere continuelle un labal qui est sur mon dialog... le seul probleme, c'est que je n'ai pas "acces" au propriété de mon label, dans la fonction du thread... Comment dois je m'y prendre?


Merci par avance

Bob...

"La chance accorde ses faveur aux esprits avertis..."

14 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
25 janv. 2005 à 13:54
Déjà modifier un contrôle depuis un autre thread que celui qui l'a crée n'est pas du tout conseillé, il faut passer par la méthode Invoke du contrôle en question.

Cocoricoooooooo !!!!
coq
MVP Visual C#
3
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
25 janv. 2005 à 16:42
Voilà : http://www.csharpfr.com/code.aspx?ID=29068

Cocoricoooooooo !!!!
coq
MVP Visual C#
3
Arthenius Messages postés 1182 Date d'inscription mercredi 21 janvier 2004 Statut Membre Dernière intervention 6 septembre 2011 14
25 janv. 2005 à 14:05
décidement c une question récurrente en ce moment la...


Arthenius
http://blogs.developpeur.org/Arthenius/

"Ce qui ne me tue pas, me rend plus fort..."
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
25 janv. 2005 à 14:53
ok, je viens de faire des recherches sur le site et il faut dire qu'on n'y trouve pas beaucoup d'info, alors pourquoi ne pas mettre une source concret, d'un prog ou on peut modififer les propriete d'un element d'une forme depuis un thread?
comme ca on aurrait un exemple concret sur le site, et cela poserai moins de probleme !
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Arthenius Messages postés 1182 Date d'inscription mercredi 21 janvier 2004 Statut Membre Dernière intervention 6 septembre 2011 14
25 janv. 2005 à 14:56
y a plus qu'a Lordbob


Arthenius
http://blogs.developpeur.org/Arthenius/

"Ce qui ne me tue pas, me rend plus fort..."
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
25 janv. 2005 à 15:03
ouais je demande pas mieux de poster la source pour aider les differentes personnes en détresse, mais j'en fais partit, et je n'ai moi meme pas compris fonctionne Invoke, c'est pour cela que je demande de l'aide ici !

Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
25 janv. 2005 à 15:57
bon ok je vais faire un petit exemple rapide

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
25 janv. 2005 à 16:48
ok je vais voir la source merci beaucoup !!!
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
25 janv. 2005 à 17:51
bon je c pas ce ke tu va dire, mais j'ai essayé d'appliquer ton code a mon programme, voila donc ma source:


using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


using System.Threading;


namespace winveil


{



/// <summary>



/// Description résumée de Form1.



/// </summary>



public
class Form1 : System.Windows.Forms.Form


{



private System.Windows.Forms.Label label1;



private System.Windows.Forms.Label label3;



private System.Windows.Forms.Label label4;



private System.Windows.Forms.Label LReveil;



private System.Windows.Forms.TextBox IDH;



private System.Windows.Forms.TextBox HDM;



private System.Windows.Forms.Button BtTest;



private System.Windows.Forms.Button BtSonnerie;



private System.Windows.Forms.Button BtActive;



private System.Windows.Forms.Label LTime;



private
delegate
void UpdateLabelDelegate (
string text );



private UpdateLabelDelegate m_upLbl;



private Thread m_monThread;



/// <summary>



/// Variable nécessaire au concepteur.



/// </summary>



private System.ComponentModel.Container components =
null;



public Form1()


{



//



// Requis pour la prise en charge du Concepteur Windows Forms



//


InitializeComponent();



//



// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent



//


}



/// <summary>



/// Nettoyage des ressources utilisées.



/// </summary>



protected
override
void Dispose(
bool disposing )


{



if( disposing )


{



if (components !=
null)


{


components.Dispose();


}


}



base.Dispose( disposing );


}


#region Code généré par le Concepteur Windows Form



/// <summary>



/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas



/// le contenu de cette méthode avec l'éditeur de code.



/// </summary>



private
void InitializeComponent()


{



this.label1 =
new System.Windows.Forms.Label();



this.LReveil =
new System.Windows.Forms.Label();



this.IDH =
new System.Windows.Forms.TextBox();



this.label3 =
new System.Windows.Forms.Label();



this.HDM =
new System.Windows.Forms.TextBox();



this.label4 =
new System.Windows.Forms.Label();



this.BtTest =
new System.Windows.Forms.Button();



this.BtSonnerie =
new System.Windows.Forms.Button();



this.BtActive =
new System.Windows.Forms.Button();



this.LTime =
new System.Windows.Forms.Label();



this.SuspendLayout();



//



// label1



//



this.label1.Location =
new System.Drawing.Point(24, 32);



this.label1.Name = "label1";



this.label1.Size =
new System.Drawing.Size(100, 16);



this.label1.TabIndex = 0;



this.label1.Text = "Heure du réveil:";



//



// LReveil



//



this.LReveil.Location =
new System.Drawing.Point(8, 8);



this.LReveil.Name = "LReveil";



this.LReveil.Size =
new System.Drawing.Size(100, 16);



this.LReveil.TabIndex = 1;



this.LReveil.Text = "Réveil inactif";



//



// IDH



//



this.IDH.Location =
new System.Drawing.Point(104, 32);



this.IDH.Name = "IDH";



this.IDH.Size =
new System.Drawing.Size(24, 20);



this.IDH.TabIndex = 2;



this.IDH.Text = "textBox1";



//



// label3



//



this.label3.Location =
new System.Drawing.Point(128, 32);



this.label3.Name = "label3";



this.label3.Size =
new System.Drawing.Size(16, 23);



this.label3.TabIndex = 3;



this.label3.Text = "h";



//



// HDM



//



this.HDM.Location =
new System.Drawing.Point(144, 32);



this.HDM.Name = "HDM";



this.HDM.Size =
new System.Drawing.Size(24, 20);



this.HDM.TabIndex = 4;



this.HDM.Text = "textBox2";



//



// label4



//



this.label4.Location =
new System.Drawing.Point(168, 32);



this.label4.Name = "label4";



this.label4.Size =
new System.Drawing.Size(24, 23);



this.label4.TabIndex = 5;



this.label4.Text = "min";



//



// BtTest



//



this.BtTest.Location =
new System.Drawing.Point(296, 40);



this.BtTest.Name = "BtTest";



this.BtTest.Size =
new System.Drawing.Size(96, 23);



this.BtTest.TabIndex = 6;



this.BtTest.Text = "Tester sonnerie";



//



// BtSonnerie



//



this.BtSonnerie.Location =
new System.Drawing.Point(296, 8);



this.BtSonnerie.Name = "BtSonnerie";



this.BtSonnerie.Size =
new System.Drawing.Size(96, 23);



this.BtSonnerie.TabIndex = 8;



this.BtSonnerie.Text = "Choix sonnerie";



//



// BtActive



//



this.BtActive.Location =
new System.Drawing.Point(296, 72);



this.BtActive.Name = "BtActive";



this.BtActive.Size =
new System.Drawing.Size(96, 23);



this.BtActive.TabIndex = 9;



this.BtActive.Text = "Activer réveil";



//



// LTime



//



this.LTime.Location =
new System.Drawing.Point(56, 72);



this.LTime.Name = "LTime";



this.LTime.TabIndex = 10;



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.ClientSize =
new System.Drawing.Size(400, 150);



this.Controls.Add(
this.LTime);



this.Controls.Add(
this.BtActive);



this.Controls.Add(
this.BtSonnerie);



this.Controls.Add(
this.BtTest);



this.Controls.Add(
this.label4);



this.Controls.Add(
this.HDM);



this.Controls.Add(
this.label3);



this.Controls.Add(
this.IDH);



this.Controls.Add(
this.LReveil);



this.Controls.Add(
this.label1);



this.Name = "Form1";



this.Text = "WinVeil";



this.ResumeLayout(
false);


}


#endregion



/// <summary>



/// Point d'entrée principal de l'application.



/// </summary>


[STAThread]



static
void Main()


{


m_upLbl =
new UpdateLabelDelegate(MaMethodeDeMajDuLabel);


Thread t =
new Thread(
new ThreadStart(ThreadHeure));


Application.Run(
new Form1());


}



// Thread qui va nous donné l'heure



public
static
void ThreadHeure()


{



while(
true)


{


label1.Invoke(m_upLbl, DateTime.Now.ToShortTimeString());


}


}



private
void MaMethodeDeMajDuLabel(
string text)


{


label1.Text = text;


}


}


}

mais j'ai toujours des erreurs de compil, tu peux m'aider?
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
25 janv. 2005 à 18:14
ThreadHeure est censée être une méthode d'instance dans notre cas, pas une méthode static => public void ThreadHeure()

Dans la méthode main ceci disparait :
m_upLbl = new UpdateLabelDelegate(MaMethodeDeMajDuLabel);
Thread t = new Thread(new ThreadStart(ThreadHeure));

Par contre dans le constructeur de la classe Form1 :
m_upLbl = new UpdateLabelDelegate(MaMethodeDeMajDuLabel);
m_monThread = new Thread(new ThreadStart(ThreadHeure));

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
27 janv. 2005 à 14:43
je suis vraiment désoler, mais j'ai toujours des problemes avec cette fonction, je te montre ce que j'ai comme code:


using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Threading;

namespace winveil
{

/// <summary>
/// Description résumée de Form1.
/// </summary>
public
class Form1 : System.Windows.Forms.Form
{
p
rivate System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.Label label4;

private System.Windows.Forms.Label LReveil;

private System.Windows.Forms.TextBox IDH;

private System.Windows.Forms.TextBox HDM;

private System.Windows.Forms.Button BtTest;

private System.Windows.Forms.Button BtSonnerie;

private System.Windows.Forms.Button BtActive;

private System.Windows.Forms.Label LTime;

private
delegate
void UpdateLabelDelegate (
string text );

private UpdateLabelDelegate m_upLbl;



/// <summary>



/// Variable nécessaire au concepteur.



/// </summary>



private System.ComponentModel.Container components =
null;



public Form1()


{



//



// Requis pour la prise en charge du Concepteur Windows Forms



//


InitializeComponent();



//



// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent



//


}



/// <summary>



/// Nettoyage des ressources utilisées.



/// </summary>



protected
override
void Dispose(
bool disposing )


{



if( disposing )


{



if (components !=
null)


{


components.Dispose();


}


}



base.Dispose( disposing );


}


#region Code généré par le Concepteur Windows Form



/// <summary>



/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas



/// le contenu de cette méthode avec l'éditeur de code.



/// </summary>



private
void InitializeComponent()


{



this.label1 =
new System.Windows.Forms.Label();



this.LReveil =
new System.Windows.Forms.Label();



this.IDH =
new System.Windows.Forms.TextBox();



this.label3 =
new System.Windows.Forms.Label();



this.HDM =
new System.Windows.Forms.TextBox();



this.label4 =
new System.Windows.Forms.Label();



this.BtTest =
new System.Windows.Forms.Button();



this.BtSonnerie =
new System.Windows.Forms.Button();



this.BtActive =
new System.Windows.Forms.Button();



this.LTime =
new System.Windows.Forms.Label();



this.SuspendLayout();



//



// label1



//



this.label1.Location =
new System.Drawing.Point(24, 32);



this.label1.Name = "label1";



this.label1.Size =
new System.Drawing.Size(100, 16);



this.label1.TabIndex = 0;



this.label1.Text = "Heure du réveil:";



//



// LReveil



//



this.LReveil.Location =
new System.Drawing.Point(8, 8);



this.LReveil.Name = "LReveil";



this.LReveil.Size =
new System.Drawing.Size(100, 16);



this.LReveil.TabIndex = 1;



this.LReveil.Text = "Réveil inactif";



//



// IDH



//



this.IDH.Location =
new System.Drawing.Point(104, 32);



this.IDH.Name = "IDH";



this.IDH.Size =
new System.Drawing.Size(24, 20);



this.IDH.TabIndex = 2;



this.IDH.Text = "textBox1";



//



// label3



//



this.label3.Location =
new System.Drawing.Point(128, 32);



this.label3.Name = "label3";



this.label3.Size =
new System.Drawing.Size(16, 23);



this.label3.TabIndex = 3;



this.label3.Text = "h";



//



// HDM



//



this.HDM.Location =
new System.Drawing.Point(144, 32);



this.HDM.Name = "HDM";



this.HDM.Size =
new System.Drawing.Size(24, 20);



this.HDM.TabIndex = 4;



this.HDM.Text = "textBox2";



//



// label4



//



this.label4.Location =
new System.Drawing.Point(168, 32);



this.label4.Name = "label4";



this.label4.Size =
new System.Drawing.Size(24, 23);



this.label4.TabIndex = 5;



this.label4.Text = "min";



//



// BtTest



//



this.BtTest.Location =
new System.Drawing.Point(296, 40);



this.BtTest.Name = "BtTest";



this.BtTest.Size =
new System.Drawing.Size(96, 23);



this.BtTest.TabIndex = 6;



this.BtTest.Text = "Tester sonnerie";



//



// BtSonnerie



//



this.BtSonnerie.Location =
new System.Drawing.Point(296, 8);



this.BtSonnerie.Name = "BtSonnerie";



this.BtSonnerie.Size =
new System.Drawing.Size(96, 23);



this.BtSonnerie.TabIndex = 8;



this.BtSonnerie.Text = "Choix sonnerie";



//



// BtActive



//



this.BtActive.Location =
new System.Drawing.Point(296, 72);



this.BtActive.Name = "BtActive";



this.BtActive.Size =
new System.Drawing.Size(96, 23);



this.BtActive.TabIndex = 9;



this.BtActive.Text = "Activer réveil";



//



// LTime



//



this.LTime.Location =
new System.Drawing.Point(56, 72);



this.LTime.Name = "LTime";



this.LTime.TabIndex = 10;



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.ClientSize =
new System.Drawing.Size(400, 150);



this.Controls.Add(
this.LTime);



this.Controls.Add(
this.BtActive);



this.Controls.Add(
this.BtSonnerie);



this.Controls.Add(
this.BtTest);



this.Controls.Add(
this.label4);



this.Controls.Add(
this.HDM);



this.Controls.Add(
this.label3);



this.Controls.Add(
this.IDH);



this.Controls.Add(
this.LReveil);



this.Controls.Add(
this.label1);



this.Name = "Form1";



this.Text = "WinVeil";



this.ResumeLayout(
false);



// Thread avec la fonction de mise a jour


m_upLbl =
new UpdateLabelDelegate(MaMethodeDeMajDuLabel);


Thread t =
new Thread(
new ThreadStart(ThreadHeure));


}


#endregion



/// <summary>



/// Point d'entrée principal de l'application.



/// </summary>


[STAThread]



static
void Main()


{


Application.Run(
new Form1());


}



// Thread qui va nous donné l'heure



public
static
void ThreadHeure()


{



while(
true)


{


label1.Invoke(m_upLbl, DateTime.Now.ToShortTimeString());


}


}



private
void MaMethodeDeMajDuLabel(
string text)


{


label1.Text = text;


}


}


}

et j'ai aussi rajouté ceci:



private
void InitializeComponent()


{



this.label1 =
new System.Windows.Forms.Label();



this.LReveil =
new System.Windows.Forms.Label();



this.IDH =
new System.Windows.Forms.TextBox();



this.label3 =
new System.Windows.Forms.Label();



this.HDM =
new System.Windows.Forms.TextBox();



this.label4 =
new System.Windows.Forms.Label();



this.BtTest =
new System.Windows.Forms.Button();



this.BtSonnerie =
new System.Windows.Forms.Button();



this.BtActive =
new System.Windows.Forms.Button();



this.LTime =
new System.Windows.Forms.Label();



this.SuspendLayout();



//



// label1



//



this.label1.Location =
new System.Drawing.Point(24, 32);



this.label1.Name = "label1";



this.label1.Size =
new System.Drawing.Size(100, 16);



this.label1.TabIndex = 0;



this.label1.Text = "Heure du réveil:";



//



// LReveil



//



this.LReveil.Location =
new System.Drawing.Point(8, 8);



this.LReveil.Name = "LReveil";



this.LReveil.Size =
new System.Drawing.Size(100, 16);



this.LReveil.TabIndex = 1;



this.LReveil.Text = "Réveil inactif";



//



// IDH



//



this.IDH.Location =
new System.Drawing.Point(104, 32);



this.IDH.Name = "IDH";



this.IDH.Size =
new System.Drawing.Size(24, 20);



this.IDH.TabIndex = 2;



this.IDH.Text = "textBox1";



//



// label3



//



this.label3.Location =
new System.Drawing.Point(128, 32);



this.label3.Name = "label3";



this.label3.Size =
new System.Drawing.Size(16, 23);



this.label3.TabIndex = 3;



this.label3.Text = "h";



//



// HDM



//



this.HDM.Location =
new System.Drawing.Point(144, 32);



this.HDM.Name = "HDM";



this.HDM.Size =
new System.Drawing.Size(24, 20);



this.HDM.TabIndex = 4;



this.HDM.Text = "textBox2";



//



// label4



//



this.label4.Location =
new System.Drawing.Point(168, 32);



this.label4.Name = "label4";



this.label4.Size =
new System.Drawing.Size(24, 23);



this.label4.TabIndex = 5;



this.label4.Text = "min";



//



// BtTest



//



this.BtTest.Location =
new System.Drawing.Point(296, 40);



this.BtTest.Name = "BtTest";



this.BtTest.Size =
new System.Drawing.Size(96, 23);



this.BtTest.TabIndex = 6;



this.BtTest.Text = "Tester sonnerie";



//



// BtSonnerie



//



this.BtSonnerie.Location =
new System.Drawing.Point(296, 8);



this.BtSonnerie.Name = "BtSonnerie";



this.BtSonnerie.Size =
new System.Drawing.Size(96, 23);



this.BtSonnerie.TabIndex = 8;



this.BtSonnerie.Text = "Choix sonnerie";



//



// BtActive



//



this.BtActive.Location =
new System.Drawing.Point(296, 72);



this.BtActive.Name = "BtActive";



this.BtActive.Size =
new System.Drawing.Size(96, 23);



this.BtActive.TabIndex = 9;



this.BtActive.Text = "Activer réveil";



//



// LTime



//



this.LTime.Location =
new System.Drawing.Point(56, 72);



this.LTime.Name = "LTime";



this.LTime.TabIndex = 10;



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.ClientSize =
new System.Drawing.Size(400, 150);



this.Controls.Add(
this.LTime);



this.Controls.Add(
this.BtActive);



this.Controls.Add(
this.BtSonnerie);



this.Controls.Add(
this.BtTest);



this.Controls.Add(
this.label4);



this.Controls.Add(
this.HDM);



this.Controls.Add(
this.label3);



this.Controls.Add(
this.IDH);



this.Controls.Add(
this.LReveil);



this.Controls.Add(
this.label1);



this.Name = "Form1";



this.Text = "WinVeil";



this.ResumeLayout(
false);



// Thread avec la fonction de mise a jour


m_upLbl =
new UpdateLabelDelegate(MaMethodeDeMajDuLabel);


Thread t =
new Thread(
new ThreadStart(ThreadHeure));


}

mais toujours une erreur !
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
27 janv. 2005 à 17:52
"ThreadHeure est censée être une méthode d'instance dans notre cas, pas une méthode static => public void ThreadHeure()"

une methode static ne peut pas acceder à un champ d'instance de la classe, ce qui est le cas de m_upLbl

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_LordBob Messages postés 2865 Date d'inscription samedi 2 novembre 2002 Statut Membre Dernière intervention 11 mai 2009 9
27 janv. 2005 à 18:01
je n'y arriverais donc jamais !!!

bon allé j'abandone !!! je vais faire mon projet dans un langage que je connais beaucoup mieux !
Bob...

"La chance accorde ses faveur aux esprits avertis..."
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
27 janv. 2005 à 18:06
lol
mais non, faut pas se decourager aussi vite

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
Rejoignez-nous