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 :

Paramètres lors de l'event ProgressChanged du BackgroundWorker


Sujet :

C#

  1. #1
    Membre expert
    Profil pro
    Dév
    Inscrit en
    Juin 2007
    Messages
    1 161
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Dév

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 161
    Points : 3 693
    Points
    3 693
    Par défaut Paramètres lors de l'event ProgressChanged du BackgroundWorker
    Bonjour,

    J'utilise un backgroundworker pour faire un (très) gros traitement. En suivant le tutoriel du site, je dois donc mettre à jour mon interface dans l'évènement ProgressChanged. La mise à jour de la barre de progression est OK mais le problème est que j'ai aussi une fenêtre TextArea qui indique à l'utilisateur l'avancement. Dans les messages qui vont être affichés, il y a des noms de fichiers qui vont être créés mais je ne les connais pas d'avance.
    Donc j'aurais besoin par exemple avec la création du fichier "nomFichier" de mettre à jour la TextBox :

    "Le fichier nomFichier a été créé."

    Bref, comment envoyer du texte à l'évènement ProgressChanged?

    Merci.

  2. #2
    Expert éminent
    Avatar de StormimOn
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    2 593
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2005
    Messages : 2 593
    Points : 7 660
    Points
    7 660
    Par défaut
    En combinant ceci et cela

  3. #3
    Membre expert
    Profil pro
    Dév
    Inscrit en
    Juin 2007
    Messages
    1 161
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Dév

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 161
    Points : 3 693
    Points
    3 693
    Par défaut
    Ça a marché merci mais là je rencontre un problème totalement inattendu. En effet, Mon évènement RunWorkerCompleted n'est jamais appelé à la fin de mon traitement... alors que tout s'est bien déroulé j'obtiens exactement ce que je veux. A la place j'obtiens une TargetInvocationException. Je pensais que c'était le fait de ma fonction car je faisais appel à de l'introspection mais non puisqu'en plaçant une messagebox à la toute dernière ligne du Do_Work, il s'affiche bien. Mais l'évènement RunWorkerCompleted n'est jamais appelé j'obtiens toujours cette exception .

    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
    private void bgwDesign_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
     
     
                string supplier = this.ucSelectProduct1.Supplier;
                string product = this.ucSelectProduct1.Product;
                string tint = this.ucSelectProduct1.Tint;
                string process = this.ucSelectProduct1.Process;
                dirExportName1 = ConfigurationManager.AppSettings["dirExportName1"];
                dirExportName2 = ConfigurationManager.AppSettings["dirExportName2"];
                dirExportChoice = ConfigurationManager.AppSettings["dirExportChoice"];
                object[] parameters = new object[] { null, supplier, product, tint, process };
     
                dal = new DalConnectionTools(this.connectionString1);
                dal2 = new DalConnectionTools(this.connectionString2);
     
                if (!Directory.Exists(this.dirExportChoice + this.dirExportName1))
                {
                    Directory.CreateDirectory(this.dirExportChoice + this.dirExportName1);
                }
                if (!Directory.Exists(this.dirExportChoice + this.dirExportName2))
                {
                    Directory.CreateDirectory(this.dirExportChoice + this.dirExportName2);
                }
    try
                {
                    BeProductOpt bpo = new BeProductOpt();
                    InfoEvent info;
     
                    //Table ADDITION
                    Array.Resize(ref parameters, parameters.Length + 2);
                    parameters.SetValue("%", 5);
                    parameters.SetValue("%", 6);
                    info = bpo.GetBesBaseArray(dal, parameters, this.dirExportChoice + this.dirExportName1, "BeasAddition", false);
                    //worker.ReportProgress((int)((double)100/(double)42), info);
                    worker.ReportProgress(50, info);
                    info = bpo.GetBesBaseArray(dal2, parameters, this.dirExportChoice + this.dirExportName2, "BeasAddition", true);
                    worker.ReportProgress(100, info);
    }
                catch (SqlException se)
                {
                    MessageBox.Show(se.Message);
                }
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                }
                else
                    MessageBox.Show("test");
            }

    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
     
    private void bgwDesign_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                InfoEvent info = (InfoEvent)e.UserState;
                if (info.IsPair)
                    UserControlEventUpdate(info.TabName, info.GetResult(), ucEventViewer2, info.DirPath);
                else
                    UserControlEventUpdate(info.TabName, info.GetResult(), ucEventViewer1, info.DirPath);
     
                this.pgbState.Value = e.ProgressPercentage;
            }
     
     
            private void bgwDesign_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    lblResult.Text = "An error has been occured : " + e.Error.Message;
                }
                else if (e.Cancelled)
                {
                    lblResult.Text = "Export cancelled";
                }
                else
                {
                    lblResult.Text = "Opération terminée ! Résultat : " + e.Result.ToString();
                }
                btnExport.Text = "Export";
                pgbState.Value = 0;
                lblResult.Text = String.Empty;
            }
     
     
            private void UserControlEventUpdate(string tabName, string[] res, UcEventViewer ucev, string dirPath)
            {
                if (res.Length > 0)
                {
                    ucev.Event = "Start loading export of the table " + tabName + "...\r\n\r\n\r\n";
                    for (int i = 0; i < res.Length; i++)
                    {
                        ucev.Event = "Creation of the file " + dirPath + res[i + 1] + "." + tabName + ".xml" + ".\r\n\r\n";
                        BeTools.SaveInFile(dirPath + res[i + 1] + "." + tabName + ".xml", res[i]);
                        i++;
                    }
                    ucev.Event = "End of export for the table " + tabName + ".\r\n\r\n\r\n\r\n\r\n";
                }
                else
                    ucev.Event = "There is not data in the database for the table " + tabName + ".\r\n\r\n\r\n\r\n\r\n";
            }
    Aucune erreur détecté mais pas d'évènement complete .

    Merci

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 19/10/2006, 18h04
  2. Réponses: 5
    Dernier message: 31/07/2006, 10h17
  3. Réponses: 5
    Dernier message: 04/07/2006, 16h05
  4. Réponses: 12
    Dernier message: 06/06/2006, 12h13
  5. Réponses: 2
    Dernier message: 09/01/2006, 09h38

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