Barre de statut et de progression compatible multithread

Contenu du snippet

Exemple de gestion de la barre de statut d'un formulaire compatible Multi-thread en Visual Studio 2005

Source / Exemple :


public FormGP()
        {
            InitializeComponent();
            // Gestion de la barre de progression
            ProgressBarAffiDelagate = new ProgressBarAffiDelagateDef(ProgressBarAffi);
            ProgressBarTimer = new System.Windows.Forms.Timer();
            // La progress barre à 100% reste 2.5 secondes
            ProgressBarTimer.Interval = 2500;
            ProgressBarTimer.Tick += new EventHandler(ProgressBarTimer_Tick);
            this.ProgressBarValue = -1;

            // Gestion du Status Label
            StatusLabelAffiDelagate = new StatusLabelAffiDelagateDef(StatusLabelAffi);
            StatusLabelTimer = new System.Windows.Forms.Timer();
            // L'affichage du texte reste 5s
            StatusLabelTimer.Interval = 5000;
            StatusLabelTimer.Tick += new EventHandler(StatusLabelTimer_Tick);
            this.StatusLabelValue = "";
        }

        #region Gestion de la barre de progression

        private delegate void ProgressBarAffiDelagateDef(int pValue);
        private ProgressBarAffiDelagateDef ProgressBarAffiDelagate;
        private System.Windows.Forms.Timer ProgressBarTimer;

        private void ProgressBarAffi(int pValue)
        {
            if (!this.toolStripProgressBar1.IsDisposed)
            {
                if (pValue < 0)
                {
                    this.toolStripProgressBar1.Value = 0;
                    this.toolStripProgressBar1.Visible = false;
                }
                else
                {
                    this.toolStripProgressBar1.Visible = true;
                    if (pValue >= 100)
                    {
                        this.toolStripProgressBar1.Value = 100;
                        this.ProgressBarTimer.Start();
                    }
                    else
                        this.toolStripProgressBar1.Value = pValue;
                }
            }
        }
        void ProgressBarTimer_Tick(object sender, EventArgs e)
        {
            this.ProgressBarValue = -1;
        }

        /// <summary>
        /// Valeur de la barre de progression multithread (-1) pour l'effacer
        /// </summary>
        public int ProgressBarValue
        {
            set
            {
                if (this.statusStrip1.InvokeRequired)
                    this.statusStrip1.Invoke(ProgressBarAffiDelagate, value);
                else
                    ProgressBarAffiDelagate(value);
            }
            get
            {
                return this.toolStripProgressBar1.Value;
            }

        }
        #endregion

        #region Gestion du Status Label

        private delegate void StatusLabelAffiDelagateDef(String pValue);
        private StatusLabelAffiDelagateDef StatusLabelAffiDelagate;
        private System.Windows.Forms.Timer StatusLabelTimer;

        private void StatusLabelAffi(String pValue)
        {
            if (!this.toolStripStatusLabel1.IsDisposed)
            {
                this.StatusLabelTimer.Start();
                this.toolStripStatusLabel1.Text = pValue;
            }
        }

        void StatusLabelTimer_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text = "";
        }

        /// <summary>
        /// Valeur Informative compatible multithread dans la barre de Status (Effacement automatique au bout de 5s)
        /// </summary>
        public String StatusLabelValue
        {
            set
            {
                if (this.statusStrip1.InvokeRequired)
                    this.statusStrip1.Invoke(StatusLabelAffiDelagate, value);
                else
                    StatusLabelAffi(value);
            }
            get
            {
                return this.toolStripStatusLabel1.Text;
            }
        }
        #endregion

A voir également