Advertisement
Brandan

Untitled

Jun 19th, 2014
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.18 KB | None | 0 0
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         BackgroundWorker1.RunWorkerAsync()
  5.     End Sub
  6.  
  7.     Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  8.         For i As Integer = 2 To 9
  9.             Timer1.Enabled = True
  10.             SetLabelText_ThreadSafe(Label1, "2 * " & (i))
  11.             Threading.Thread.Sleep(500)
  12.         Next
  13.     End Sub
  14.  
  15.  
  16.     ' The delegate
  17.     Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)
  18.  
  19.     ' The delegates subroutine.
  20.     Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)
  21.         ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
  22.         ' If these threads are different, it returns true.
  23.         If [Label].InvokeRequired Then
  24.             Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
  25.             Me.Invoke(MyDelegate, New Object() {[Label], [text]})
  26.         Else
  27.             [Label].Text = [text]
  28.         End If
  29.     End Sub
  30.  
  31.  
  32.  
  33. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement