Background worker sur form differente.

Résolu
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013 - 10 oct. 2011 à 16:09
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013 - 10 oct. 2011 à 21:27
Bonjour à tous,
J'ai un soucis avec mon background worker.

J'ai ma form principal (mainform) qui contien une listbox avec des items.
Lorsque l'on clique sur le bouton de la mainform, celle-ci active une autre form (que l'ont va appeler dataform).

la dataform contien un backgroundworker qui est sencé recuperer les informations de la mainform.

Apres tests, je pense que le backgroundworker n'arrive pas a aller chercher ces "items" dans la listbox.

voici en gros le code de la dataform :

   Private Sub SaverButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaverButton.Click
        BackgroundWorkerSave.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorkerSave_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerSave.DoWork
        Try
            If MainForm.CheckBox1.Checked = True Then
                If System.IO.Directory.Exists(MainForm.TextBox2.Text) Then
                    My.Computer.FileSystem.CopyDirectory(MainForm.TextBox2.Text, MainForm.TextBox0.Text + "\Custom Paths\Path1 - " & MainForm.PathNameText1.Text & "", True)
                End If
            End If
            If MainForm.CheckBox2.Checked = True Then
                If System.IO.Directory.Exists(MainForm.TextBox3.Text) Then
                    My.Computer.FileSystem.CopyDirectory(MainForm.TextBox3.Text, MainForm.TextBox0.Text + "\Custom Paths\Path2 - " & MainForm.PathNameText2.Text & "", True)
                End If
            End If
            If MainForm.CheckBox3.Checked = True Then
                If System.IO.Directory.Exists(MainForm.TextBox4.Text) Then
                    My.Computer.FileSystem.CopyDirectory(MainForm.TextBox4.Text, MainForm.TextBox0.Text + "\Custom Paths\Path3 - " & MainForm.PathNameText3.Text & "", True)
                End If
            End If
            If MainForm.CheckBox4.Checked = True Then
                If System.IO.Directory.Exists(MainForm.TextBox5.Text) Then
                    My.Computer.FileSystem.CopyDirectory(MainForm.TextBox5.Text, MainForm.TextBox0.Text + "\Custom Paths\Path4 - " & MainForm.PathNameText4.Text & "", True)
                End If
            End If
            If MainForm.CheckBox5.Checked = True Then
                If System.IO.Directory.Exists(MainForm.TextBox6.Text) Then
                    My.Computer.FileSystem.CopyDirectory(MainForm.TextBox6.Text, MainForm.TextBox0.Text + "\Custom Paths\Path5 - " & MainForm.PathNameText5.Text & "", True)
                End If
                MainForm.Refresh()
            End If

            If MainForm.ListBoxGames.Items.Contains("LeNom") Then
                If System.IO.Directory.Exists(LeNom) = True Then
                    My.Computer.FileSystem.CopyDirectory(LeNom, Destination + "\AutreDestination", True)
                    MainForm.Refresh()
                End If
            End If
end sub


Que devrai-je faire ?
Merci :)

3 réponses

The Meteorologist Messages postés 232 Date d'inscription jeudi 18 janvier 2007 Statut Membre Dernière intervention 3 novembre 2011 1
10 oct. 2011 à 19:33
Attention ! Le BackgroundWorker n'est pas utilisé il devrait l'être.

Le BackgroundWorker exécute son code sur un thread séparé (en parallèle). Donc il accède a des éléments d'un autre thread qui sont susceptibles d'être utilisé à ce moment précis.

Voici comment faire ça proprement et en toute sécurité :

Lorsque tu appelles la méthode RunWorkerAsync(), tu as la possibilité de passer un argument comme ceci :

  
 Private Sub SaverButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaverButton.Click
        BackgroundWorkerSave.RunWorkerAsync(MonArgument)
 End Sub


Dans le contexte de ton application, cet argument devrait contenir les valeurs des CheckBoxes. Tu pourrais passer un tableau de Boolean ou une classe, stucture contenant les paramètres.

Pour récupérer une information lorsque le BackgroundWorker travaille, tu peux utiliser la méthode ReportProgress(pourcentage, paramètre). Laquelle pourra renseigner ton Form sur l'avancement du boulot.

Enfin pour le résultat final peut être passé via RunWorkerCompleted() de la même manière.

Pour plus d'info :

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Simon
3
NHenry Messages postés 15112 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 13 avril 2024 159
10 oct. 2011 à 18:51
Bonjour,

Une exception ?
Sinon regardes du coté des Cross Thread Operations.

---------------------------------------------------------------------
[list=ordered][*]Pour poser correctement une question et optimiser vos chances d'obtenir des réponses, pensez à lire le règlement CS et aussi ce lien[*]Quand vous postez un code, merci d'utiliser la coloration syntaxique (3ième icône en partant de la droite : )
[*]Si votre problème est résolu (et uniquement si c'est le cas), pensez à mettre "Réponse acceptée" sur le ou les messages qui vous ont aidés./list
---
Mon site
0
Sentynel Messages postés 85 Date d'inscription jeudi 7 janvier 2010 Statut Membre Dernière intervention 13 juillet 2013
10 oct. 2011 à 21:27
Merci Meteorologist :)
0
Rejoignez-nous