BackgroundWorker pour un Progress Bar et les contrôls d'une autre form
Bonjour,
C'est la premère fois que je veux utiliser le composant BackgroundWorker
La situation est la suivante :
j'ai une form1 que je doit choisir un mois et je clique sur le boton Ok -> un traitement se fait (l'execution d'un procédure stockée qui peu durée jusqu'a 10 second) avec une ProgressBar puis -> Afficher le resultat dans un datagridview d'une autre form2
et mon code est :
pour le Bouton "OK"
Code:
1 2 3 4 5 6 7
| Private Sub BtOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtOk.Click
backgroundWorker1 = New BackgroundWorker
backgroundWorker1.WorkerReportsProgress = True
backgroundWorker1.WorkerSupportsCancellation = True
backgroundWorker1.RunWorkerAsync()
End Sub |
Et pour le DoWork
Code:
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
| Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker1.DoWork
Dim Frm As New Form2
With Frm
.DataGListe.DataSource = .BindingSourceListe
Try
TableListe = New DataTable
Cmd = New SqlCommand("Compta.P_Balance_Generale", Connexion)
Cmd.CommandType = CommandType.StoredProcedure
.SqlDAListe = New SqlDataAdapter(Cmd)
.SqlDAListe.Fill(TableListe)
.BindingSourceListe.DataSource = TableListe
.LbNbr.Text = TableListe.Rows.Count
......
.DataGListe.Rows(0).DefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)
For Each Colonne As DataGridViewColumn In .DataGListe.Columns
Colonne.SortMode = DataGridViewColumnSortMode.NotSortable
Next
Catch ex As Exception
End Try
.Show()
End With
Dim nbr As Integer = 400
Dim compteur As Integer = 0
Dim delegateIncrement As dIncrement = AddressOf Increment
Do
compteur = delegateIncrement(compteur)
backgroundWorker1.ReportProgress((compteur * 100) / nbr)
If Me.backgroundWorker1.CancellationPending = True Then
e.Result = compteur
Exit Do
End If
Loop Until compteur = nbr
e.Result = compteur
End Sub |
Et dans le load du Form2
Code:
1 2 3 4 5 6 7
| Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim TextBox1 As TextBox
Dim TextBox2 As TextBox
End Sub |
mais j'ai le problème que Form2 se plante
et si je mets en commentaire
La ProgressBar Progresse normal
Merci de m'eclairer ce problème