Newbie Qestion - Multithreading
I'm currently playing with multithreading in vb.net and I can run one thread with no problem. If I try to run two threads, it seems to run slowly for some strange reason. I don't know how to expain it. Maybe the code i'm using will help(wo/ form code):
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim t1 As System.Threading.Thread
Dim t2 As System.Threading.Thread
#Region " Windows Form Designer generated code "
Private Sub BackgroundProcess1()
'//setting up the first subroutine that will run in thread
Dim i As Integer = 1
Do While True
TextBox1.Text = "Iterations: " & i
i += 1
Loop
End Sub
Private Sub BackgroundProcess2()
'//setting up the second subroutine that will run in thread
Dim i2 As Integer = 1
Do While True
TextBox2.Text = "Iterations: " & i2
i2 += 1
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'//assigning subroutine to the thread and starting thread
t1 = New System.Threading.Thread(AddressOf Me.BackgroundProcess1)
t2 = New System.Threading.Thread(AddressOf Me.BackgroundProcess2)
t1.Start()
t2.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//setting up button that will stop and start thread
If Button1.Text = "Stop" Then
t1.Suspend()
t2.Suspend()
Button1.Text = "Start"
ElseIf Button1.Text = "Start" Then
t1.Resume()
t2.Resume()
Button1.Text = "Stop"
End If
End Sub
End Class