Calling Main Thread from a worker thread in vb.net Documentation

P

Peter

I found this awesome example of calling a Main Thread from a worker
thread on abstractvb.com but I read through the example and would like
to see if anyone can clarify some of this code below:


Questions:

1. The Public Sub New proceedure. Is that just a proceedure named
new or is there more to this? I went through the code and I do not
see any refrences to 'New'

2.Does anyone have any really good links to threading articles. The
microsoft links that I have are not clearly illustrated.

Imports System.Threading

Public Delegate Sub CallBack(ByVal intTotal As Integer, ByVal
strThreadName As String)

Public Class Counter
Private LabelToUpdate As Label
Private MaxCount As Integer
Private CallBackMethod As CallBack

Public Sub New(ByVal l As Label, ByVal intMax As Integer, ByVal cb
As CallBack)
LabelToUpdate = l
MaxCount = intMax
CallBackMethod = cb
End Sub

Public Sub BeginProcessing()
Dim i As Integer

For i = 1 To MaxCount
LabelToUpdate.Text = i
LabelToUpdate.Refresh()
Next

CallBackMethod(i - 1, Thread.CurrentThread.Name)
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim c1 As New Counter(Label1, 3000, AddressOf ThreadCompleted)
Dim c2 As New Counter(Label2, 1000, AddressOf ThreadCompleted)

Dim t1 As New Thread(AddressOf c1.BeginProcessing)
Dim t2 As New Thread(AddressOf c2.BeginProcessing)

t1.Name = "Counter Thread 1"
t2.Name = "Counter Thread 2"

t1.Start()
t2.Start()
End Sub

Private Sub ThreadCompleted(ByVal intValue As Integer, ByVal strName
As String)
MsgBox("Thread " & strName & " has completed at " & intValue)
End Sub
 
P

peter

Found my answers:

Public New is the class requirements.

And Callback returns the values.

Scary. I'm starting to understand this subject.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top