| 12
 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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 
 |  
Imports System.ComponentModel
Imports System.Threading
 
Public Class Form1
    Private indexBoucle As Integer = 10000000
    Private plusgrandPourcentageAtteint() As Integer
    Private percentComplete() As Integer
    Private pbTab As List(Of ProgressBar) = New List(Of ProgressBar)
    Private lbTab As List(Of Label) = New List(Of Label)
    Private bwTab As List(Of BackgroundWorker) = New List(Of BackgroundWorker)
 
    Private NbThread As Integer = 5
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b1 As New Panel
 
        b1.Size = New Drawing.Size(470, 370)
        b1.AutoScroll = True
 
        Me.AutoScroll = True
 
        Me.Controls.Add(b1)
 
        Dim x As Integer = 140
        Dim y As Integer = 20
 
        'ReDim pbTab(NbThread)
        'ReDim lbTab(NbThread)
        'ReDim bwTab(NbThread)
 
        Dim Names As ArrayList = New ArrayList()
        Names.Add("Toto")
        Names.Add("Titi")
        Names.Add("Tata")
        Names.Add("Tutu")
        Names.Add("Toutou")
        Names.Add("Teuteu")
 
 
        For I = 0 To NbThread
 
            pbTab.Add(New ProgressBar)
            lbTab.Add(New Label)
            bwTab.Add(New BackgroundWorker)
 
            bwTab(I).WorkerReportsProgress = True
            'supporte l'annulation
            bwTab(I).WorkerSupportsCancellation = True
            AddHandler bwTab(I).DoWork, AddressOf bwTab_DoWork
            AddHandler bwTab(I).ProgressChanged, AddressOf bwTab_ProgressChanged
            AddHandler bwTab(I).RunWorkerCompleted, AddressOf bwTab_RunWorkerCompleted
 
            pbTab(I).Style = ProgressBarStyle.Blocks
            pbTab(I).Maximum = 100
 
            pbTab(I).Size = New Drawing.Size(300, 20)
            lbTab(I).Size = New Drawing.Size(150, 20)
 
            pbTab(I).Location = New System.Drawing.Point(x, y)
            lbTab(I).Location = New System.Drawing.Point(x - 120, y)
 
            lbTab(I).Text = Names(I)
 
            y = y + 30
 
            b1.Controls.Add(pbTab(I))
            b1.Controls.Add(lbTab(I))
 
            bwTab(I).RunWorkerAsync(indexBoucle)
 
        Next I
        ' Reset the variable for percentage tracking.
        ReDim Me.plusgrandPourcentageAtteint(Me.NbThread)
        ReDim Me.percentComplete(Me.NbThread)
        btnCancelAsyncAll.Enabled = True
    End Sub
 
    Private Sub bwTab_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Trace.WriteLine("Thread : " & e.Argument & " started...")
        ' Get the BackgroundWorker object that raised this event.
        Dim worker As BackgroundWorker = _
            CType(sender, BackgroundWorker)
        If bwTab.Contains(worker) Then
            e.Result = maTache(e.Argument, worker, e)
        End If
 
    End Sub
 
    Private Sub bwTab_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
 
        If bwTab.Contains(worker) Then
            Dim numBW As Integer = bwTab.IndexOf(worker)
            Me.pbTab(numBW).Value = e.ProgressPercentage
        End If
 
 
    End Sub
 
    Private Sub bwTab_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim numBW As Integer = -1
        If bwTab.Contains(worker) Then
            numBW = bwTab.IndexOf(worker)
 
        End If
 
        ' First, handle the case where an exception was thrown.
        If (e.Error IsNot Nothing) Then
            MessageBox.Show(e.Error.Message)
        ElseIf e.Cancelled Then
            Me.lbTab(numBW).Text = "Canceled"
        Else
            Me.lbTab(numBW).Text = e.Result.ToString()
 
        End If
 
 
        Trace.WriteLine("Thread finished")
    End Sub
    Private Function maTache(ByVal n As Integer, _
        ByVal worker As BackgroundWorker, _
        ByVal e As DoWorkEventArgs) As Long
 
        If n < 0 OrElse n > indexBoucle Then
            Throw New ArgumentException( _
                "value must be >= 0 and <= 1000", "n")
        End If
        Dim result As Long = 0
        'ici tu dois rajouter eventuellement un bouton cancel
        'pour appeler pour appeler l'event cancelAsync
 
 
        If worker.CancellationPending Then
            e.Cancel = True
        Else
            Dim numBW As Integer = Me.bwTab.IndexOf(worker)
            For i As Integer = 0 To n
                ' Report progress as a percentage of the total task.
                percentComplete(numBW) = i * 100 / n
                If percentComplete(numBW) > plusgrandPourcentageAtteint(numBW) Then
                    plusgrandPourcentageAtteint(numBW) = percentComplete(numBW)
                    worker.ReportProgress(percentComplete(numBW))
                End If
                result = i
            Next
        End If
        Return result
 
 
    End Function
 
    Private Sub btnCancelAsyncAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelAsyncAll.Click
        For Each objBW As BackgroundWorker In Me.bwTab
            ' Cancel the asynchronous operation.
            objBW.CancelAsync()
 
            ' Disable the Cancel button.
            btnCancelAsyncAll.Enabled = False
        Next
 
    End Sub
End Class | 
Partager