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 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
| Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Threading
Namespace ThreadTest
Public Class Form1
Inherits System.Windows.Forms.Form
Private Delegate Sub InvokeMethod(ByVal [step] As Integer)
Private progressBar1 As System.Windows.Forms.ProgressBar
Private button1 As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.progressBar1 = New System.Windows.Forms.ProgressBar()
Me.button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
Me.progressBar1.Location = New System.Drawing.Point(8, 16)
Me.progressBar1.Name = "progressBar1"
Me.progressBar1.Size = New System.Drawing.Size(344, 24)
Me.progressBar1.TabIndex = 3
Me.button1.Location = New System.Drawing.Point(424, 8)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(96, 40)
Me.button1.TabIndex = 2
Me.button1.Text = "GO"
AddHandler Me.button1.Click, AddressOf Me.button1_Click
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(544, 285)
Me.Controls.Add(Me.progressBar1)
Me.Controls.Add(Me.button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim a As Integer
Dim objCOntrol As UserControl
Me.progressBar1.Minimum = 0
Me.progressBar1.Maximum = 10
Dim t As New Thread(New ThreadStart(AddressOf ThreadProcess))
t.Start()
For intcompteur As Integer = 1 To 1000000000
a = a + 1
Next
objCOntrol = New UserControl
objCOntrol.BackColor = Color.Red
Me.Controls.Add(objCOntrol)
End Sub
Private Sub ThreadProcess()
Dim res As IAsyncResult
Dim i As Integer = 0
While i < 11
res = progressBar1.BeginInvoke(New InvokeMethod(AddressOf UpdateProgressBar), New Object() {i})
progressBar1.EndInvoke(res)
Thread.Sleep(1000)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Sub
Private Sub UpdateProgressBar(ByVal [step] As Integer)
Me.progressBar1.Value = [step]
End Sub
End Class
End Namespace |
Partager