Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.15 KB | None | 0 0
  1. Imports System.Threading
  2.  
  3. Public Class Form1
  4.     Private Sub Button1_Click() Handles Button1.Click
  5.         ' Display a messagebox with the UI thread ID
  6.         MessageBox.Show($"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}")
  7.         ' create a new thread to perform the long running task
  8.         Dim t As New Thread(AddressOf DoWork)
  9.         t.IsBackground = True
  10.         t.Start()
  11.     End Sub
  12.  
  13.     Private Sub DoWork()
  14.         MessageBox.Show($"Long task starting. Secondary Thread ID {Thread.CurrentThread.ManagedThreadId}")
  15.         ' Pretend to perform a long running task off the UI thread. This will stop the UI from freezing.
  16.         Thread.Sleep(5000)
  17.         ' Pretend long task finished.
  18.         ' To access the forms UI thread we must invoke back to the UI thread.
  19.         Me.Invoke(Sub()
  20.                       ' now we are back on the UI thread.
  21.                       MessageBox.Show($"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}")
  22.                       Me.Text = $"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}"
  23.                   End Sub)
  24.     End Sub
  25. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement