Meka][Meka Unstopable Posts: 700
| note: you may use this tutorial on other sites as long as u post the author note. -------------------------- Basic Manual Threading -------------------------- Author: Meka][Meka -------------------------- http://www.meka-meka.com/ -------------------------- Level: Beginner -------------------------- Ok start a new project, first we will make a sample app without threading
Code: | Dim WithEvents tmr As Timer = New Timer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmr.Interval = 1000 '1 second tmr.Start() 'give app time to load End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick tmr.Enabled = False RunLoop() End Sub Private Sub RunLoop() For i As Integer = 0 To 10000000 Me.Text = "TEST [" & i & "]" Next End Sub | ok when u run this, either your window will freeze and u cant move it, until the numbers are counted all the way, or you wont see the numbers but be able to move the app, this is because the main thread is busy, this is standard, so now lets add some threading
Code: | Dim WithEvents tmr As Timer = New Timer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmr.Interval = 1000 '1 second tmr.Start() 'give app time to load End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick tmr.Enabled = False Dim thread As New Threading.Thread(AddressOf RunLoop) thread.IsBackground = True 'best for looping thru things constantly etc thread.Start() End Sub Private Sub RunLoop() For i As Integer = 0 To 10000000 Me.Text = "TEST [" & i & "]" Next End Sub | ok when you run it this time u will see that u can now move the window and be able to see the numbers increase lets try another app using mulit adding
Code: | Dim WithEvents tmr As Timer = New Timer Dim l1, l2 As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmr.Interval = 1000 '1 second tmr.Start() 'give app time to load End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick tmr.Enabled = False RunLoop() RunSecondLoop() End Sub Private Sub RunLoop() 'counts in 1's For i As Integer = 0 To 10000000 l1 = "TESTA [" & i & "]" UpdateMe() Next End Sub Private Sub RunSecondLoop() 'counts in 2's For i As Integer = 0 To 10000000 l2 = "TESTB [" & i + 2 & "]" UpdateMe() Next End Sub Private Sub UpdateMe() Me.Text = l1 & " " & l2 End Sub | it is recommended u dont try run this unless u have a pretty fast cpu, it will freeze the app totally to much for the single thread to handle, now lets add 1 extra thread
Code: | Dim WithEvents tmr As Timer = New Timer Dim l1, l2 As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmr.Interval = 1000 '1 second tmr.Start() 'give app time to load End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick tmr.Enabled = False Dim Thread(1) As Threading.Thread '2 threads Thread(0) = New Threading.Thread(AddressOf RunLoop) Thread(1) = New Threading.Thread(AddressOf RunSecondLoop) Thread(0).IsBackground = True Thread(1).IsBackground = True Thread(0).Start() Thread(1).Start() End Sub Private Sub RunLoop() 'counts in 1's For i As Integer = 0 To 10000000 l1 = "TESTA [" & i & "]" UpdateMe() Next End Sub Private Sub RunSecondLoop() 'counts in 2's For i As Integer = 0 To 10000000 l2 = "TESTB [" & i + 2 & "]" UpdateMe() Next End Sub Private Sub UpdateMe() Me.Text = l1 & " " & l2 End Sub | ok with this ther is no freeze, but we cant really see the +2 difference, so lets sleep 1 of the threads, lets kill the first one
Code: | Dim WithEvents tmr As Timer = New Timer Dim l1, l2 As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tmr.Interval = 1000 '1 second tmr.Start() 'give app time to load End Sub Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick tmr.Enabled = False Dim Thread(1) As Threading.Thread '2 threads Thread(0) = New Threading.Thread(AddressOf RunLoop) Thread(1) = New Threading.Thread(AddressOf RunSecondLoop) Thread(0).IsBackground = True Thread(1).IsBackground = True Thread(0).Start() Thread(1).Start() End Sub Private Sub RunLoop() 'counts in 1's For i As Integer = 0 To 10000000 l1 = "TESTA [" & i & "]" UpdateMe() Threading.Thread.CurrentThread.Sleep(10) Next End Sub Private Sub RunSecondLoop() 'counts in 2's For i As Integer = 0 To 10000000 l2 = "TESTB [" & i + 2 & "]" UpdateMe() Next End Sub Private Sub UpdateMe() Me.Text = l1 & " " & l2 End Sub | well that concludes the basic Threading, hope u find it useful, njoy -/Meka][Meka
|