Thread en c#
Résolu
moi411
-
moi411 Messages postés 179 Date d'inscription Statut Membre Dernière intervention -
moi411 Messages postés 179 Date d'inscription Statut Membre Dernière intervention -
Bonsoir tout le monde,
Je viens de faire iun programme visant à récupérer dans une textbox, tous les chemin des fichiers texte de mon disque dur au fur et à mesure de la recherche, un peu comme le programme de recherche sous windows (la finalité étant de faire un programme complet de recherche de fichiers).
En expérimentant mon programme je me suis appeçu que mon thread ne fonctionne pas... Il n'affiche rien pourtant il appelle bien la bonne fonction!
Tout ce que je veux ici, c'est afficher le répertoire ou l'ordinateur cherche pendant qu'il affiche les fichiers dans une listview, d'ou l'utilité du thread (si j'ai bien compris!).
Ci-joint le code, en espérant que quelqu'un va m'aider...
Merci d'avance et à bientôt.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//déclaré ici car besoin dans AfficherChemin() (voir Thread)
string nomFichier;
private void button1_Click(object sender, EventArgs e)
{
//rep ou commencer la recherche
DirectoryInfo rep = new DirectoryInfo(@"C:\");
//Thread servant à afficher les fichier trouvé par Chercher(rep);
Thread t = new Thread(new ThreadStart(AfficherChemin));
t.Name = "TChemin";
t.Start();
//fonction de recherche (récurcive)
Chercher(rep);
}
public void Chercher(DirectoryInfo Rep)
{
//récupère le chemin complet du fichier
nomFichier=Rep.FullName;
//dans le répertoire courant on cherche tous les fichiers
//dont l'extension est .txt
foreach (FileInfo f in Rep.GetFiles("*.txt"))
{
//et on on crée un item avec les chemeins et tailles de ces fichiers
//item qui sera ajouté à la listview
ListViewItem it = new ListViewItem();
it.Text = f.FullName;
it.SubItems.Add(f.Length.ToString());
listView1.Items.Add(it);
}
//si il y a des sous répertoires on rappelle "Chercher();" (principe de la récursivité)
DirectoryInfo[] a = Rep.GetDirectories();
foreach (DirectoryInfo b in a)
{
Chercher(b);
}
}
//le thread appelle cette fonction qui affiche
//dans une textBox multiligne, tous les chemins
//des fichier texte sur un disque (C:).
public void AfficherChemin()
{
while (true)
{
textBox1.Text = nomFichier;
Thread.Sleep(0);
}
}
}
}
Je viens de faire iun programme visant à récupérer dans une textbox, tous les chemin des fichiers texte de mon disque dur au fur et à mesure de la recherche, un peu comme le programme de recherche sous windows (la finalité étant de faire un programme complet de recherche de fichiers).
En expérimentant mon programme je me suis appeçu que mon thread ne fonctionne pas... Il n'affiche rien pourtant il appelle bien la bonne fonction!
Tout ce que je veux ici, c'est afficher le répertoire ou l'ordinateur cherche pendant qu'il affiche les fichiers dans une listview, d'ou l'utilité du thread (si j'ai bien compris!).
Ci-joint le code, en espérant que quelqu'un va m'aider...
Merci d'avance et à bientôt.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//déclaré ici car besoin dans AfficherChemin() (voir Thread)
string nomFichier;
private void button1_Click(object sender, EventArgs e)
{
//rep ou commencer la recherche
DirectoryInfo rep = new DirectoryInfo(@"C:\");
//Thread servant à afficher les fichier trouvé par Chercher(rep);
Thread t = new Thread(new ThreadStart(AfficherChemin));
t.Name = "TChemin";
t.Start();
//fonction de recherche (récurcive)
Chercher(rep);
}
public void Chercher(DirectoryInfo Rep)
{
//récupère le chemin complet du fichier
nomFichier=Rep.FullName;
//dans le répertoire courant on cherche tous les fichiers
//dont l'extension est .txt
foreach (FileInfo f in Rep.GetFiles("*.txt"))
{
//et on on crée un item avec les chemeins et tailles de ces fichiers
//item qui sera ajouté à la listview
ListViewItem it = new ListViewItem();
it.Text = f.FullName;
it.SubItems.Add(f.Length.ToString());
listView1.Items.Add(it);
}
//si il y a des sous répertoires on rappelle "Chercher();" (principe de la récursivité)
DirectoryInfo[] a = Rep.GetDirectories();
foreach (DirectoryInfo b in a)
{
Chercher(b);
}
}
//le thread appelle cette fonction qui affiche
//dans une textBox multiligne, tous les chemins
//des fichier texte sur un disque (C:).
public void AfficherChemin()
{
while (true)
{
textBox1.Text = nomFichier;
Thread.Sleep(0);
}
}
}
}
A voir également:
- Thread en c#
- Compte thread - Accueil - Réseaux sociaux
- Thread instagram c'est quoi - Accueil - Guide réseaux sociaux
- Fausse appli Threads : déjà une imitation frelatée et dangereuse ! - Accueil - Guide arnaque
- C quoi thread - Guide
- Thread instagram - Guide
2 réponses
Au cas ou qqun aurait besoin de cette réponse, je connais qqun qui a finalement pu m'aider et donc je mais le code fonctionnel, à tous hasards...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//délégation servant à afficher dans la textbox
delegate void AjoutTextBoxText(string Texte);
public void AjoutTextBox(string Texte)
{
textBox1.Text = Texte;
}
//Même chose avec la listview
delegate void AjoutListViewItem(string chem, string taille);
public void AjoutListView(string chem, string taille)
{
ListViewItem it = new ListViewItem();
it.Text = chem;
it.SubItems.Add(taille);
listView1.Items.Add(it);
}
private void button1_Click(object sender, EventArgs e)
{
//rep ou commencer la recherche
DirectoryInfo rep = new DirectoryInfo(@"C:\");
//Thread servant commencer la recherche
Thread t = new Thread(new ThreadStart(AfficherChemin));
t.Name = "TChemin";
t.Start();
}
public void Chercher(DirectoryInfo Rep)
{
try
{
//récupère le chemin complet du dossier où se trouve le fichier
AjoutTextBoxText ajoutText = new AjoutTextBoxText(AjoutTextBox);
Invoke(ajoutText, new object[] { Rep.FullName });
//récupère le nom du fichier ainsi que sa taille
AjoutListViewItem ajoutItem = new AjoutListViewItem(AjoutListView);
//dans le répertoire courant on cherche tous les fichiers
//dont l'extension est .txt
foreach (FileInfo f in Rep.GetFiles("*.txt"))
{
Invoke(ajoutItem, new object[] { f.FullName, f.Length.ToString() });
}
}
catch { }
try
{
//si il y a des sous répertoires on rappelle "Chercher();" (principe de la récursivité)
DirectoryInfo[] a = Rep.GetDirectories();
foreach (DirectoryInfo b in a)
{
Chercher(b);
}
}
catch { }
}
//le thread appelle cette fonction qui cherche les fichiers texte sur un disque (C:).
public void AfficherChemin()
{
DirectoryInfo rep = new DirectoryInfo(@"C:\");
Chercher(rep);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//délégation servant à afficher dans la textbox
delegate void AjoutTextBoxText(string Texte);
public void AjoutTextBox(string Texte)
{
textBox1.Text = Texte;
}
//Même chose avec la listview
delegate void AjoutListViewItem(string chem, string taille);
public void AjoutListView(string chem, string taille)
{
ListViewItem it = new ListViewItem();
it.Text = chem;
it.SubItems.Add(taille);
listView1.Items.Add(it);
}
private void button1_Click(object sender, EventArgs e)
{
//rep ou commencer la recherche
DirectoryInfo rep = new DirectoryInfo(@"C:\");
//Thread servant commencer la recherche
Thread t = new Thread(new ThreadStart(AfficherChemin));
t.Name = "TChemin";
t.Start();
}
public void Chercher(DirectoryInfo Rep)
{
try
{
//récupère le chemin complet du dossier où se trouve le fichier
AjoutTextBoxText ajoutText = new AjoutTextBoxText(AjoutTextBox);
Invoke(ajoutText, new object[] { Rep.FullName });
//récupère le nom du fichier ainsi que sa taille
AjoutListViewItem ajoutItem = new AjoutListViewItem(AjoutListView);
//dans le répertoire courant on cherche tous les fichiers
//dont l'extension est .txt
foreach (FileInfo f in Rep.GetFiles("*.txt"))
{
Invoke(ajoutItem, new object[] { f.FullName, f.Length.ToString() });
}
}
catch { }
try
{
//si il y a des sous répertoires on rappelle "Chercher();" (principe de la récursivité)
DirectoryInfo[] a = Rep.GetDirectories();
foreach (DirectoryInfo b in a)
{
Chercher(b);
}
}
catch { }
}
//le thread appelle cette fonction qui cherche les fichiers texte sur un disque (C:).
public void AfficherChemin()
{
DirectoryInfo rep = new DirectoryInfo(@"C:\");
Chercher(rep);
}
}