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

Windows Forms Discussion :

Thread + component + Refresh qui ne se fait pas


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Février 2007
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 21
    Par défaut Thread + component + Refresh qui ne se fait pas
    J'ai une classe :

    public partial class StatusBarWaiting : ToolStripControlHost{........}

    J'ajoute ceci dans ma statusbar. Je démarre avec une méthode start. La méthode start lance un thread qui loop en déplacant une progress bar et en affichant du text. J'ai mis les InvokeRequired afin de ne pas avoir le Cross Thread. Cependant, je réussi a voir mon affichage seulement si je quitte la form et que je reviens dedans (mon statusbar est dans une tab, je dois changer et revenir).

    J'ai essayé de mettre des .Refresh()... rien n'a faire. Pourquoi est-ce que le control ne se rafraichi pas?

    Merci

    Voici le code du start():

    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
     
    public void start()
            {
                if (this.Control.InvokeRequired)
                {
                    this.Control.BeginInvoke(new delCrossThreadMethod(start));
                    return;
                }
     
                this.pgbProgress.Value = 0;
                this.pgbProgress.Visible = true;
     
                threadProgressBar = new Thread(new ThreadStart(threadMoveProgressBar));
                threadProgressBar.Name = "Progress";
                threadProgressBar.Start();
                this.timeKeeperActionList.start();
    Et pour bouger la progressbar:

    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
     
            /// <summary>
            /// Loop that invoke to the good thread the move of the
            /// progress bar
            /// </summary>
            private void threadMoveProgressBar()
            {
                waitingPersistence = true;
     
                while (waitingPersistence)
                {
                    Thread.Sleep(200);
                    moveProgressBar();
                }
            }
     
            /// <summary>
            /// Move the progress bar of 1 unit
            /// </summary>
            private void moveProgressBar()
            {
                if (this.Control != null)
                {
                    if (this.Control.InvokeRequired)
                    {
                        this.Control.BeginInvoke(new delCrossThreadMethod(moveProgressBar));
                        return;
                    }
                    this.pgbProgress.Value = pgbProgress.Value % pgbProgress.Maximum + 1;
     
                    this.Control.Refresh();
                    this.pgbProgress.Refresh();
                }
            }
    Vous-avez une idée?

  2. #2
    Membre averti
    Inscrit en
    Février 2007
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 21
    Par défaut
    Voici le code en entier. Une fois que vous l'avez compiler, vous allez pouvoir l'ajouter à une statusbar. Pour démarrer : start() pour arrêter stop().

    Lors du start(), si vous redimensionnez la fenêtre, vous allez remarquer que le progress bar bouge. Si vous ométez de redimensionnez la fenêtre rien ne se fait à l'écran!

    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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
     
    /// <summary>
        /// Class that handle an effect when something need to wait.
        /// 
        /// http://en.csharp-online.net/Tool,_Menu,_and_Status_Strips%E2%80%94Creating_a_Custom_ToolStripItem
        /// </summary>
        [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip |
        ToolStripItemDesignerAvailability.StatusStrip)]
        public partial class StatusBarWaiting : ToolStripControlHost
        {
            /// <summary>
            /// Delegate method used for the cross thread operation
            /// </summary>
            private delegate void delCrossThreadMethod();
     
            private FlowLayoutPanel controlPanel;
            private Label lblNumberRows = new Label();
            private Label lblTimes = new Label();
            private ProgressBar pgbProgress = new ProgressBar();
     
            /// <summary>
            /// Thread that show progress. Especially the progress of the
            /// threadPersistence above. This thread have stop condition
            /// related to the state of life of threadPersistence.
            /// </summary>
            private Thread threadProgressBar;
            /// <summary>
            /// Record the time for loading the action list.
            /// </summary>
            private TimeKeeper timeKeeperActionList;
     
            /// <summary>
            /// Waiting progress bar
            /// </summary>
            private bool waitingPersistence;
     
            private string textNumberResult = String.Empty;
     
            public StatusBarWaiting():base(new FlowLayoutPanel())
            {
                InitializeComponent();
                this.timeKeeperActionList = new TimeKeeper();
     
                // Set up the FlowLayouPanel.
                controlPanel = (FlowLayoutPanel)base.Control;
                controlPanel.BackColor =System.Drawing.Color.Transparent;
     
                this.ResultNumber = 0;
                lblTimes.Text = "0 seconds";
                pgbProgress.Style = ProgressBarStyle.Continuous;
                pgbProgress.Maximum = 50;
                pgbProgress.Visible = false;
                pgbProgress.Size = new System.Drawing.Size(150, 12);
     
     
                controlPanel.Controls.Add(lblNumberRows);
                controlPanel.Controls.Add(lblTimes);
                controlPanel.Controls.Add(pgbProgress);
     
     
            }
     
            public void start()
            {
                if (this.Control.InvokeRequired)
                {
                    this.Control.BeginInvoke(new delCrossThreadMethod(start));
                    return;
                }
     
                this.pgbProgress.Value = 0;
                this.pgbProgress.Visible = true;
     
                threadProgressBar = new Thread(new ThreadStart(threadMoveProgressBar));
                threadProgressBar.Name = "Progress";
                threadProgressBar.Start();
                this.timeKeeperActionList.start();
     
                //this.controlPanel.ResumeLayout(true);
                //this.pgbProgress.ResumeLayout(true);
     
                //this.controlPanel.Update();
                //this.controlPanel.Refresh();
     
                //this.pgbProgress.Refresh();
                //this.pgbProgress.Update();
     
            }
     
            public void stop()
            {
                if (this.Control != null)
                {
                    if (this.Control.InvokeRequired)
                    {
                        this.Control.BeginInvoke(new delCrossThreadMethod(stop));
                        return;
                    }
                    try
                    {
                        this.timeKeeperActionList.stop();
                        this.pgbProgress.Value = 0;
                        this.pgbProgress.Visible = false;
     
                        if (threadProgressBar != null)
                        {
                            threadProgressBar.Abort();
                            threadProgressBar = null;
                            waitingPersistence = false;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.getInstance().add(new Feedback(Log.LOG_ERROR, "StatusBarWaiting internal error : " + e.Message));
                    }
                }
            }
     
            /// <summary>
            /// Loop that invoke to the good thread the move of the
            /// progress bar
            /// </summary>
            private void threadMoveProgressBar()
            {
                waitingPersistence = true;
     
                while (waitingPersistence)
                {
                    Thread.Sleep(200);
                    moveProgressBar();
                }
            }
     
            /// <summary>
            /// Move the progress bar of 1 unit
            /// </summary>
            private void moveProgressBar()
            {
                if (this.Control != null)
                {
                    if (this.Control.InvokeRequired)
                    {
                        this.Control.BeginInvoke(new delCrossThreadMethod(moveProgressBar));
                        return;
                    }
                    this.pgbProgress.Value = pgbProgress.Value % pgbProgress.Maximum + 1;
     
                    this.Control.Refresh();
                    this.pgbProgress.Refresh();
                }
            }
     
            public int ResultNumber
            {
                set
                {
                    if (value>1)
                        this.textNumberResult = (value + " results");
                    else
                        this.textNumberResult = (value + " result");
     
                    updateNumberRow();
                }
            }
     
           private void updateNumberRow()
            {
                if (this.Control != null)
                {
                    if (this.Control.InvokeRequired)
                    {
                        this.Control.BeginInvoke(new delCrossThreadMethod(updateNumberRow));
                        return;
                    }
                    this.lblNumberRows.Text = this.textNumberResult;
                }
            }
     
     
     
        }

  3. #3
    Membre averti
    Inscrit en
    Février 2007
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 21
    Par défaut
    J'ai mis ma progress bar visible = true dès le départ et ca fonctionne. Si je débute avec visible = false et que je la mets a visible = true lors du start(), rien affiche... bizarre.

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 46
    Par défaut
    Bonjour,

    Je n'ai pas pu tester ton code mais as tu essayés :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    System.Windows.Forms.Application.DoEvents();
    Par exemple à la fin de ta méthode Start();

    Bigy

Discussions similaires

  1. [Mail] Envoi d'e-mail qui ne se fait pas
    Par sam01 dans le forum Langage
    Réponses: 3
    Dernier message: 26/07/2007, 10h41
  2. rsync : sauvegarde incrementale qui ne se fait pas
    Par nixonne dans le forum Shell et commandes GNU
    Réponses: 1
    Dernier message: 22/07/2007, 12h11
  3. [Tableaux] création de tableau qui ne se fait pas
    Par mussara dans le forum Langage
    Réponses: 2
    Dernier message: 01/11/2006, 21h44
  4. Test qui ne se fait pas
    Par GLDavid dans le forum Linux
    Réponses: 12
    Dernier message: 07/03/2006, 14h57
  5. Pb de selection qui ne se fait pas
    Par Stef.proxi dans le forum Langage SQL
    Réponses: 4
    Dernier message: 06/08/2004, 10h54

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