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 :

[C# THREADS]problème avec backgroundworker


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut [C# THREADS]problème avec backgroundworker
    Bonsoir,

    J'ai eu envie de tester après lecture de ce tutoriel : http://glarde.developpez.com/dotnet/bgworker/cs/

    J'ai donc créé une micro application (vraiment micro )

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace bgWorkerTest1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 1; i <= 10; i++)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        label1.Text = "opération annulée";
                    }
                    else
                    {
                        backgroundWorker1.ReportProgress(i * 10);
                        Thread.Sleep(1500);                
                    }
     
                }
            }
     
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
                label1.Text = String.Format("avancement : {0}", e.ProgressPercentage.ToString());
            }
     
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                button1.Enabled = true;
                MessageBox.Show("Fini");
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                this.button1.Enabled = false;
                backgroundWorker1.RunWorkerAsync();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                backgroundWorker1.CancelAsync();
            }
        }
    }
    mais j'obtiens l'erreur explicitée dans le tutoriel


    J'ai beau chercher , j'ai bien compris que le label appartient à un autre thread mais alors comment afficher mon texte "opération annulée" ?

    Merci de votre aide

  2. #2
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    essaie de travailler avec les delegates

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Citation Envoyé par chamamo
    essaie de travailler avec les delegates
    merci, mais je suis vraiment un noob, serait-il possible que tu me donnes un exemple s'il te plait ?

  4. #4
    Membre expérimenté Avatar de Mose
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 143
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 143
    Points : 1 379
    Points
    1 379
    Par défaut
    Y'a un post sur ce "problème" environ une fois toutes les deux semaines.

  5. #5
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    un bon tutorial sur les delegués
    http://dev-lang.over-blog.com/article-272885.html

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    j'ai, je pense, bien chercher, et vous remercie de votre aide, j'ai donc tenter les delegates comme ceci :

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace bgWorkerTest1
    {
        public partial class Form1 : Form
        {
     
            delegate void Delaffiche();
     
            void afficher()
            {
                label1.Text = "Opération annulée";
                Thread.Sleep(1500);
            }
     
     
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 1; i <= 10; i++)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        Delaffiche dell = new Delaffiche(afficher);
                        dell();
                    }
                    else
                    {
                        backgroundWorker1.ReportProgress(i * 10);
                        Thread.Sleep(1500);                
                    }
     
                }
            }
     
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
                label1.Text = String.Format("avancement : {0}", e.ProgressPercentage.ToString());
            }
     
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                button1.Enabled = true;
                MessageBox.Show("Fini");
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                this.button1.Enabled = false;
                backgroundWorker1.RunWorkerAsync();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                backgroundWorker1.CancelAsync();
            }
        }
    }
    mais j'ai encore la même erreur ...

    Merci tout de même de votre aide

  7. #7
    Membre averti
    Avatar de dtavan
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    162
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 162
    Points : 381
    Points
    381
    Par défaut
    presque ca

    ton délégué :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    delegatevoidDelaffiche();
    void afficher()
    {
    label1.Text = "Opération annulée";
    button2.Enabled = false;
    progressBar1.Value = 0;
    }
    
    ensuite appelle comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if (backgroundWorker1.CancellationPending)
    {
    e.Cancel = true;
    //Delaffiche dell = new Delaffiche(afficher);
    //dell();
    this.Invoke(newDelaffiche(afficher));
    }
    
    voilà tiens nous au courant
    David Tavan

    Mon blog 1
    Mon blog 2

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Citation Envoyé par dtavan
    presque ca

    ton délégué :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    delegatevoidDelaffiche();
    void afficher()
    {
    label1.Text = "Opération annulée";
    button2.Enabled = false;
    progressBar1.Value = 0;
    }
    
    ensuite appelle comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    if (backgroundWorker1.CancellationPending)
    {
    e.Cancel = true;
    //Delaffiche dell = new Delaffiche(afficher);
    //dell();
    this.Invoke(newDelaffiche(afficher));
    }
    
    voilà tiens nous au courant

    Merci ca fonctionne mais si je peux me permettre quelle est la différence entre Invoke et BeginInvoke ?

  9. #9
    Membre averti
    Avatar de dtavan
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    162
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 162
    Points : 381
    Points
    381
    David Tavan

    Mon blog 1
    Mon blog 2

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 16/10/2008, 09h48
  2. Problème avec BackgroundWorker
    Par supasonic77 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 27/06/2007, 11h40
  3. Réponses: 11
    Dernier message: 14/02/2006, 00h26
  4. Thread--> problème avec ThreadProc
    Par stof dans le forum MFC
    Réponses: 33
    Dernier message: 08/06/2005, 13h47
  5. Réponses: 5
    Dernier message: 10/05/2005, 10h22

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