backgroundWorker1.WorkerReportsProgress = True AddHandler backgroundWorker1.ProgressChanged, AddressOf backgroundWorker1_ProgressChanged
Private Sub LancerLeThread() Dim monThread As New Thread(AddressOf LaMethodeDuThread) monThread.Start() End Sub Private monDispatcher As Dispatcher = Dispatcher.CurrentDispatcher Private Sub LaMethodeDuThread() For i As Integer = 0 To Integer.MaxValue - 1 If i Mod 1e3 = 0 Then monDispatcher.BeginInvoke(New Action(Sub() label1.Text = i.ToString())) End If Thread.Sleep(1) Next i End Sub
Option Strict On Option Explicit On Imports System.ComponentModel Public Class Form1 Dim bw As BackgroundWorker = New BackgroundWorker Delegate Sub SetLabel1(text As String) Delegate Sub SetDataGridView1(ByVal row As Integer, ByVal cell As Integer) Public Shared Sub Main() Application.Run(New Form1()) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler bw.DoWork, AddressOf bw_DoWork AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted Me.PictureBox1.Visible = False End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Not bw.IsBusy = True Then Me.PictureBox1.Visible = True bw.RunWorkerAsync() End If End Sub Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) SetLabel1_Delegate("Load ...") For i As Integer = 1 To 35000 SetDataGridView1_Delegate(i, 35001 - i) Next End Sub Private Sub SetLabel1_Delegate(ByVal text As String) Dim newtext As String = text If Me.Label1.InvokeRequired Then Dim d As New SetLabel1(AddressOf SetLabel1_Delegate) Me.Invoke(d, New Object() {newtext}) Else Label1.Text = newtext Me.Refresh() End If End Sub Private Sub SetDataGridView1_Delegate(ByVal row As Integer, ByVal cell As Integer) If Me.DataGridView1.InvokeRequired Then Dim d As New SetDataGridView1(AddressOf SetDataGridView1_Delegate) Me.Invoke(d, New Object() {row, cell}) Else Dim newrow As String() = New String() {CStr(row), CStr(cell)} DataGridView1.Rows.Add(newrow) End If End Sub Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Me.PictureBox1.Visible = False Me.Label1.Text = "Done !!!" End Sub End Class
Espace de noms: System.Windows.Threading
Assembly: WindowsBase (dans WindowsBase.dll)
Imports System.Windows.Threadingprovoque une erreur.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question