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# Discussion :

Thread qui bloque l'affichage de la form [Débutant]


Sujet :

C#

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut Thread qui bloque l'affichage de la form
    Salut,
    je débute en C# et je travaille sur un petit logiciel MultiFonction avec un SplashScreen au démarrage:

    Ma classe LoadFunction.cs:
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace MultiThing.Splash
    {
        class LoadFunction
        {
            FSplash splash = new FSplash();
            public void LoadLibrairies()
            {
                splash.txtLoad.Text = "Chargement des librairies...";
                splash.txtLoad.Refresh();
                splash.barLoad.Value = 0;
            }
            public void LoadFiles()
            {
                splash.txtLoad.Text = "Chargement des fichiers...";
                splash.barLoad.Value = 20;
            }
            public void LoadPlugins()
            {
                splash.txtLoad.Text = "Chargement des plugins...";
                splash.barLoad.Value = 40;
            }
            public void LoadPrefs()
            {
                splash.txtLoad.Text = "Chargement des préférences utilisateurs...";
                splash.barLoad.Value = 60;
            }
            public void LoadCache()
            {
                splash.txtLoad.Text = "Mise en cache...";
                splash.barLoad.Value = 80;
            }
            public void Starting()
            {
                splash.txtLoad.Text = "C'est parti !";
                splash.barLoad.Value = 100;
            }
        }
    }
    Ma form FSplash.cs:
    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Timers;
    using MultiThing.Splash;
     
    namespace MultiThing
    {
        public partial class FSplash : Form
        {
     
            public FSplash()
            {
                LoadFunction Load = new LoadFunction();
                InitializeComponent();
                Thread.Sleep(2000);
                Load.LoadLibrairies();
                Thread.Sleep(2000);
                Load.LoadFiles();
                Thread.Sleep(2000);
                Load.LoadPlugins();
                Thread.Sleep(2000);
                Load.LoadPrefs();
                Thread.Sleep(2000);
                Load.LoadCache();
                Thread.Sleep(2000);
                Load.Starting();
                Thread.Sleep(2000);
            }
        }
    }
    Vous allez dire que sa charge rien mais oui car je n'ai pas encore developpé tous le reste du programme ^^
    Et enfete sa affiche ma form que apprès avoir fini les Thread.Sleep, 14 secondes après donc avec le message "C'est parti !"
    J'ai aussi essayé avec un Timer mais je n'ai plus le code sous la main et j'avais des erreurs.
    Merci d'avance
    PS: J'espère être dans la bonne catégorie ^^

  2. #2
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Bonjour,

    Cela est normal car tu es dans le main thread, il faut que tu lance un thread pour exécuter ton chargement toutes les 2s et non en patientant 14s .

    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
     
    public Thread chargementFonction;
     
    public FSplash()
    {
              InitializeComponent();
              chargementFonction = new Thread(chargement);
              chargementFonction.Start();
    }
     
    public void chargement()
    {
              //Appel de tes fonctions.....
              chargementFonction.Abort();
    }

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Salut,
    Merci de ta réponse et voila mon code modifié sauf que chargementFonction.start me provoque
    Une exception de type 'System.OutOfMemoryException' a été levée.
    en faisant freezé VS C#, mon programme !
    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
    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Timers;
    using MultiThing.Splash;
     
    namespace MultiThing
    {
        public partial class FSplash : Form
        {
            public Thread chargementFonction;
            public FSplash()
            {
                InitializeComponent();
                chargementFonction = new Thread(chargement);
                chargementFonction.Start();
            }
            public void chargement()
            {
                LoadFunction Load = new LoadFunction();
                Thread.Sleep(2000);
                Load.LoadLibrairies();
                Thread.Sleep(2000);
                Load.LoadFiles();
                Thread.Sleep(2000);
                Load.LoadPlugins();
                Thread.Sleep(2000);
                Load.LoadPrefs();
                Thread.Sleep(2000);
                Load.LoadCache();
                Thread.Sleep(2000);
                Load.Starting();
                Thread.Sleep(2000);
                chargementFonction.Abort();
            }
        }
    }
    Merci

  4. #4
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Je doute que ca soit la mémoire vive la cause, mais quel volume charges tu?
    Relance l'application en regardant la gestion mémoire vive pour voir si elle est saturée.

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Au début il ne freeze pas, il se lance mais la progressbar ou le label ne change pas et au bout de 5 secondes c'est le freeze de VisualStudio et de MultiThing
    et quand sa freeze MultiThing utilise 754 mo de ram mais il m'en reste 1 Gb de libre ( les autres utilisés par chrome, skype et compagnie ) Et j'ai 4gb de ram sur un i3 cadencé a 3.2Ghz sous W7 Home Prenium x64
    Merci
    EDIT: Et je suis a 77% utilisé

  6. #6
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Et autre chose, pour afficher à partir d'un thread il vaut mieux procédé de la façon suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private delegate void Afficher(string str, int charg);
    public void Affichertext(string str, int charg)
    {
                splash.txtLoad.Text = str;
                splash.txtLoad.Refresh();
                splash.barLoad.Value = charg;
     
    public void chargement()
    {
                this.Invoke(new Afficher(Affichertext), "chargement des librairies", 0);
    }

  7. #7
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    En suivant ta méthode voici ce que j'obtient:
    Impossible d'appeler Invoke ou BeginInvoke sur un contrôle tant que le handle de fenêtre n'a pas été créé.
    LoadFunction.cs:
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace MultiThing.Splash
    {
        class LoadFunction
        {
            FSplash splash = new FSplash();
            public void Affichertext(string str, int charg)
            {
                splash.txtLoad.Text = str;
                splash.txtLoad.Refresh();
                splash.barLoad.Value = charg;
            }
        }
    }
    FSplash.cs

    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Timers;
    using MultiThing.Splash;
     
    namespace MultiThing
    {
        public partial class FSplash : Form
        {
            public Thread chargementFonction;
            public FSplash()
            {
                InitializeComponent();
                chargementFonction = new Thread(chargementx);
                chargementFonction.Start();
            }
            private delegate void Afficher(string str, int charg);
            public void chargementx()
            {
                LoadFunction Load = new LoadFunction();
                this.Invoke(new Afficher(Load.Affichertext), "Chargement des librairies...", 0);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Load.Affichertext), "Chargement des fichiers...", 20);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Load.Affichertext), "Chargement des plugins...", 40);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Load.Affichertext), "Chargement des préférences utilisateurs...", 60);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Load.Affichertext), "Mise en cache...", 80);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Load.Affichertext), "C'est parti !", 100);
                chargementFonction.Abort();
            }
        }
    }
    Merci
    EDIT: après une recherche j'ai rajoutez ca:
    if (!this.IsHandleCreated) { this.CreateHandle(); }, mais maintenant j'ai cette erreur:
    Opération inter-threads non valide : le contrôle 'FSplash' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé.
    notamment sur splash.txtLoad.Text = str;

  8. #8
    Membre éclairé
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    610
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Septembre 2011
    Messages : 610
    Points : 713
    Points
    713
    Par défaut
    Alors pour cette erreur il y a 2 solutions:
    - La première qui n'est pas propre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Control.CheckForIllegalCrossThreadCalls = false
    - La seconde beaucoup plus propre qui utilise InvokeRequired

  9. #9
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 4
    Points
    4
    Par défaut
    Merci FrameBreak pour ton aide !
    J'ai finalement déplace ma fonction AfficherText dans FSplash.cs vu que un fichier pour une fonction c'est inutile et sa fonctionne ^^
    Si jamais des personnes passe par la:
    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Timers;
     
    namespace MultiThing
    {
        public partial class FSplash : Form
        {
            public Thread chargementFonction;
            public FSplash()
            {
                InitializeComponent();
                chargementFonction = new Thread(chargement);
                chargementFonction.Start();
            }
            private delegate void Afficher(string str, int charg);
            public void chargement()
            {
                if (!this.IsHandleCreated) { this.CreateHandle(); }
     
                this.Invoke(new Afficher(Affichertext), "Chargement des librairies...", 0);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Affichertext), "Chargement des fichiers...", 20);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Affichertext), "Chargement des plugins...", 40);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Affichertext), "Chargement des préférences utilisateurs...", 60);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Affichertext), "Mise en cache...", 80);
                Thread.Sleep(2000);
                this.Invoke(new Afficher(Affichertext), "C'est parti !", 100);
                chargementFonction.Abort();
            }
            public void Affichertext(string str, int charg)
            {
     
                txtLoad.Text = str;
                txtLoad.Refresh();
                barLoad.Value = charg;
            }
        }
    }
    Merci Beaucoup !!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 09/07/2010, 09h59
  2. [QThread] Main thread qui bloque un autre thread
    Par Amnell dans le forum Multithreading
    Réponses: 6
    Dernier message: 25/02/2010, 15h38
  3. problème de Thread qui bloque ma Servlet
    Par need2learn dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 19/11/2008, 09h39
  4. Affichage d'une form a partir d'un thread
    Par spy1 dans le forum C#
    Réponses: 1
    Dernier message: 26/05/2007, 23h06
  5. Arreter un thread qui "bloque" sur un socket
    Par J-F dans le forum Concurrence et multi-thread
    Réponses: 2
    Dernier message: 12/12/2006, 00h04

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