Controle listbox dans un thread

Résolu
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007 - 20 nov. 2006 à 17:53
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007 - 28 nov. 2006 à 12:11
bonjour ,

je souhaite afficher des nombres premiers dans une listbox à travers un thread

voici mon code sans erreur de compilation mais qui n'affiche rien quelqu'un a t il une idée ?

merci.

using

 System;
using

 System.Collections.Generic;
using

 System.ComponentModel;
using

 System.Data;
using

 System.Drawing;
using

 System.Text;
using

 System.Windows.Forms;
using

 System.Threading; 

namespace

 Premier{

public
partial
class
Form1 : 
Form{

public Form1(){

InitializeComponent(); 

}

[

STAThread]

public
static
void lancePremier(){

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(
false);

Application.Run(
new
Form1());

Thread t = 
new
Thread(
new
ThreadStart(ThreadFunction));t.Start();

}

private
static
void ThreadFunction(){

Form1 f = 
new
Form1();

//f.listBox1.BeginUpdate();

for (
int p = 1; p < 50; p++){

int i = 2;

while ((p % i) != 0 &amp;&amp; i < p){

i++;

}

if (i == p){

f.listBox1.Items.Add(p.ToString());

Thread.Sleep(50);}

}

//f.listBox1.EndUpdate();}

12 réponses

MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
20 nov. 2006 à 18:10
Salut

Voila ton code corrigé

<hr />using System;

using System.Drawing;

using System.Windows.Forms;

using System.Threading;

namespace Premier
{

   public
delegate
void
AddItemDelegateHandler(
ListBox lb,
string item);

   public
class
Form1 :
Form
  {

      private
ListBox listBox1;

      private
AddItemDelegateHandler AddItemDelegate;

      
      public
static
void Main()
      {

         Application.Run(
new
Form1());
      }

      
      public Form1()
      {
         InitializeComponent();

         this.AddItemDelegate =
new
AddItemDelegateHandler(AddItem);

         this.HandleCreated +=
new
EventHandler(Form1_HandleCreated);
      }

      
      private
void Form1_HandleCreated(
object sender,
EventArgs e)
      {

         Thread t =
new
Thread(
new
ThreadStart(ThreadFunction));
         t.Start();
      }

      public
void AddItem(
ListBox lb,
string item)
      {
            lb.Items.Add(item);
       }

       private
void InitializeComponent()
       {

            this.listBox1 =
new System.Windows.Forms.
ListBox();

            this.SuspendLayout();

            this.listBox1.FormattingEnabled =
true;

            this.listBox1.Location =
new System.Drawing.
Point(22, 22);

            this.listBox1.Name =
"listBox1";

            this.listBox1.Size =
new System.Drawing.
Size(177, 212);

            this.listBox1.TabIndex = 0;

            this.ClientSize =
new System.Drawing.
Size(284, 264);

            this.Controls.Add(
this.listBox1);

            this.ResumeLayout(
false);
         }

         private
void ThreadFunction()
         {

               for (
int p = 1; p < 50; p++)
               {

                  int i = 2;

                  while ((p % i) != 0 && i < p) i++;

                  if (i == p)
this.Invoke(AddItemDelegate,
this.listBox1, i.ToString());
               }
         }
   }
}
<hr />

Mx
MVP C# 
3
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
28 nov. 2006 à 11:07
Salut

remplace 
   public void  Form1()
par
   public Premier() // Constructeur de ta classe Premier()

Mx
MVP C# 
3
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
20 nov. 2006 à 18:09
Salut,
Pas super la mise en forme de ton texte
Ca ne marche surement pas pour des raisons de cross-threading. Si t'as pas d'exception c'est certainement que tu travailles avec le framework 1.
Tu peux jeter un oeil à ce tuto de Mx qui parle du cross-threading

-Blog-
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
20 nov. 2006 à 18:11
Quand on parle du loup....

-Blog-
0

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

Posez votre question
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
20 nov. 2006 à 18:15
^^

En fait, ce n'est meme pas une question de cross-threading quand on regarde mieux le code d'origine.
La Form est lancée dans le Thread...

Mx
MVP C# 
0
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007
20 nov. 2006 à 19:26
désolé pour la mise en page c'est le réflexe du tag [code]

merci pour la solution c'est bien ça et le tuto sur l'utilisation de delegate car je manquais totalement d'info sur le sujet.
0
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007
27 nov. 2006 à 11:44
petite question complémentaire :

ce code premier qui est maintenant sous forme de dll est dans une autre application qui doit pouvoir appeler ce thread à partir d'un menu dans le genre :

private

void lancerToolStripMenuItem1_Click(
object sender,
EventArgs e){

Console.WriteLine(
"lancement d'un thread Premier");

Premier obj2 =
new
Premier();
obj2.Form1();

}

le problème est que cette fois la form premier ne s'affiche plus et on ne lance pas le thread.Etant encore débutant en C# je ne vois pas le problème, pouvez vous m'aider?
merci
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
27 nov. 2006 à 11:50
Salut

en reprenant le code que je t'ai donné, il suffirait de faire

Form1 f = new Form1();
f.Show();

(et d'enlever la méthode Main)

Mx
MVP C# 
0
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007
27 nov. 2006 à 12:16
ça me crée bien une form mais pas celui qui contient la listbox
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
27 nov. 2006 à 12:20
Tu peux me donner ton code pour que je vois exactement quel appel tu dois faire ?

Mx
MVP C# 
0
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007
27 nov. 2006 à 16:32
voici donc la partie qui est dans la dll :






using



System;
u

sing
System.Collections.Generic;


using
System.ComponentModel;


using
System.Data;


using
System.Drawing;


using
System.Text;


using
System.Windows.Forms;


using
System.Threading;


namespace
GestionThreads
{


public



delegate



void



AddItemDelegateHandler
(

ListBox
lb,

string
item);



public



partial



class



Premier
: System.Windows.Forms.

Form

{


private



ListBox
listBox1;


private



AddItemDelegateHandler
AddItemDelegate;


public



void
Form1()
{
InitializeComponent();


this
.AddItemDelegate =

new



AddItemDelegateHandler
(AddItem);


this
.HandleCreated +=

new



EventHandler
(Form1_HandleCreated);
}


private



void
Form1_HandleCreated(

object
sender,

EventArgs
e)
{


Thread
t =

new



Thread
(

new



ThreadStart
(ThreadFunction));
t.Start();
}


public



void
AddItem(

ListBox
lb,

string
item)
{
lb.Items.Add(item);
}


public



void
ThreadFunction()
{


for
(

int
p = 1; p < 1000; p++)
{


int
i = 2;


while
((p % i) != 0 && i < p) i++;


if
(i == p)

this
.Invoke(AddItemDelegate,

this
.listBox1, i.ToString());
}


private



void
InitializeComponent()
{


this
.listBox1 =

new
System.Windows.Forms.

ListBox
();


this
.SuspendLayout();


// listBox1

//

this
.listBox1.FormattingEnabled =

true
;


this
.listBox1.Location =

new
System.Drawing.

Point
(18, 18);


this
.listBox1.Name =

"listBox1"
;


this
.listBox1.Size =

new
System.Drawing.

Size
(124, 225);


this
.listBox1.TabIndex = 0;


//

// Premier

//

this
.ClientSize =

new
System.Drawing.

Size
(157, 266);


this
.Controls.Add(

this
.listBox1);


this
.Name =

"Premier"
;


this
.Text =

"Premier"
;


this
.ResumeLayout(

false
);
}
}
}





puis la partie de l'appli qui appelle le thread qui est juste un menu pour le moment :





using



System;


using
System.Collections.Generic;



using
System.ComponentModel;


using
System.Data;


using
System.Drawing;


using
System.Text;


using
System.Windows.Forms;


using
System.Threading;


using
GestionThreads;


namespace



GestionThreads
{


public



partial



class



Threads
:

Form

{
p

ublic
Threads()
{
InitializeComponent();
}


private



void
lancerToolStripMenuItem1_Click(

object
sender,

EventArgs
e)
{


Console
.WriteLine(

"lancement d'un thread Premier"
);


Premier
obj2 =

new



Premier
();


obj2.Show();
}
}
}
0
cs_diego29 Messages postés 22 Date d'inscription mardi 3 juin 2003 Statut Membre Dernière intervention 17 octobre 2007
28 nov. 2006 à 12:11
merci beaucoup c'était cela
0
Rejoignez-nous