PC Review


Reply
Thread Tools Rate Thread

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

 
 
Peter
Guest
Posts: n/a
 
      1st Nov 2004
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
 
Reply With Quote
 
 
 
 
peter
Guest
Posts: n/a
 
      1st Nov 2004
Found my answers:

Public New is the class requirements.

And Callback returns the values.

Scary. I'm starting to understand this subject.


"Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking a method from the main thread, from a worker thread. DaTurk Microsoft C# .NET 6 30th Aug 2007 08:07 PM
Calling a main thread method from a worker thread Hao L Microsoft C# .NET 5 2nd May 2005 08:31 PM
Calling main thread from a worker thread Chad Miller Microsoft VB .NET 0 13th Jan 2005 03:54 PM
Calling main thread from a worker thread Chad Miller Microsoft VB .NET 1 13th Jan 2005 03:14 PM
Worker Thread raising events to the main thread? ORC Microsoft Dot NET Compact Framework 5 24th Nov 2004 03:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:19 PM.