Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Threading
- Public Class Form1
- Private Sub Button1_Click() Handles Button1.Click
- ' Display a messagebox with the UI thread ID
- MessageBox.Show($"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}")
- ' create a new thread to perform the long running task
- Dim t As New Thread(AddressOf DoWork)
- t.IsBackground = True
- t.Start()
- End Sub
- Private Sub DoWork()
- MessageBox.Show($"Long task starting. Secondary Thread ID {Thread.CurrentThread.ManagedThreadId}")
- ' Pretend to perform a long running task off the UI thread. This will stop the UI from freezing.
- Thread.Sleep(5000)
- ' Pretend long task finished.
- ' To access the forms UI thread we must invoke back to the UI thread.
- Me.Invoke(Sub()
- ' now we are back on the UI thread.
- MessageBox.Show($"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}")
- Me.Text = $"Long task starting. UI Thread ID {Thread.CurrentThread.ManagedThreadId}"
- End Sub)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement