Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

2 février 2014 7 02 /02 /février /2014 01:34

http://msdn.microsoft.com/fr-fr/library/system.componentmodel.backgroundworker(v=vs.110).aspx

public BackgroundWorker backgroundWorker1;

 

        private void button1_Click(object sender, EventArgs e)

        {

            // Using System.ComponentModel;

            // la variable contenant le nombre d'opérations à effectuer

            int operationCount=Convert.ToInt32(textBox1.Text);

 

            progressBar1.Minimum = 0;

            progressBar1.Step = 1;

            progressBar1.Value = 0;

            progressBar1.Maximum =operationCount ;

 

 

            backgroundWorker1 = new BackgroundWorker() { WorkerReportsProgress = true };

            backgroundWorker1.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);

            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);

            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);

 

            // on passe la variable en argument

            backgroundWorker1.RunWorkerAsync(operationCount);

        }

 

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

        {

          

            for (int i = 0; i <= (int)e.Argument; i++)

            {

                Thread.Sleep(500); // pour la démo

                int n = i * 100 / ((int)e.Argument);

                backgroundWorker1.ReportProgress(n);

            }

        }

 

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            label1.Text = string.Format("{0} %",e.ProgressPercentage.ToString());

            this.progressBar1.PerformStep();

 

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            MessageBox.Show("Fini !!");

        }

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires