G
Guest
I trying to learn how multithread works. I found an example in C# and I am
trying to make it work in VB, but I am having problems with it. Could
someone help me to make it work. Here is the code I am working with. Why is
the main thread executed first and them the other?
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim thr As Thread
thr = New Thread(AddressOf ProcessMsg)
thr.Start()
Call MainThread()
End Sub
Private Delegate Sub WriteMsg(ByVal value As String)
Private Sub PostMsg(ByVal value As String)
Me.TextBox1.Text += "Secondary Thread: " & value & vbNewLine
End Sub
Private Sub ProcessMsg()
Dim i As Long
For i = 0 To 5
Me.Invoke(New WriteMsg(AddressOf PostMsg), New Object()
{i.ToString})
Next
End Sub
Private Sub MainThread()
Dim i As Long
For i = 0 To 5
Me.TextBox1.Text += "Main Thread: " & i.ToString & vbNewLine
Me.TextBox1.Refresh()
Thread.Sleep(350)
Next
End Sub
End Class
trying to make it work in VB, but I am having problems with it. Could
someone help me to make it work. Here is the code I am working with. Why is
the main thread executed first and them the other?
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim thr As Thread
thr = New Thread(AddressOf ProcessMsg)
thr.Start()
Call MainThread()
End Sub
Private Delegate Sub WriteMsg(ByVal value As String)
Private Sub PostMsg(ByVal value As String)
Me.TextBox1.Text += "Secondary Thread: " & value & vbNewLine
End Sub
Private Sub ProcessMsg()
Dim i As Long
For i = 0 To 5
Me.Invoke(New WriteMsg(AddressOf PostMsg), New Object()
{i.ToString})
Next
End Sub
Private Sub MainThread()
Dim i As Long
For i = 0 To 5
Me.TextBox1.Text += "Main Thread: " & i.ToString & vbNewLine
Me.TextBox1.Refresh()
Thread.Sleep(350)
Next
End Sub
End Class