473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Nov 21 '05 #1
1 1590
Found my answers:

Public New is the class requirements.

And Callback returns the values.

Scary. I'm starting to understand this subject.
"Peter" <pe***@mclinn.com> wrote in message
news:dc*************************@posting.google.co m...
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

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
0
by: Ted Miller | last post by:
Hi folks, I originally posted this on microsoft.public.dotnet.framework.interop. Reposting to broaden the audience. I'm having an interop problem where my managed component is reentered from...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
5
by: Hao L | last post by:
For example, void WorkerMethod() { ... UnregisterAllHotkeys();} void UnregisterAllHotKeys { for(...) {UnregisterHotKey(...);}} UnregisterHotKey is an API function that must be on the thread...
1
by: Chad Miller | last post by:
I know haw to call a the main thread from a worker thread in a windows form class, but how does a worker thread call its main thread from a none windows form class? Thank You. ...
2
by: Tim | last post by:
The are 2 threads - main thread and worker thread. The main thread creates the worker thread and then the worker thread runs in an infinite while loop till it is asked to exit by setting an event....
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
1
by: bytebugs | last post by:
Hi, As a matter of principle I have refranied from creating an instance of Form or UserControl from within a class that does not inherit from System.Windows.Form. I am looking for comments on...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.