inherits thread class

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

I am new to vb.net and just playing around to get a feel
for it. I am coming from a vb6/java background so please
excuse me if my idea is plain stupid or contra good
programming practice in dot net. In java a class can
implement runnable and then the run method is started as a
new thread. I want to attempt to do the same thing in
vb.net but have struggled to find any examples about doing
anything like this. I know I could spawn a thread as
normal in a class but was just thinking ( dangerous I am
usually told :-) ) about a more elegant solution.

Regards,

Michael
 
Hi,

I don't think inheritance is appropriate. Simply use the Threading
namespace (and create a class that imports it). You would inherit if you
wanted to extend the Threading class (namespace) in some significant way.
All that you want to do is to employ free threading. IMO, inheritance adds
nothing except complexity to this scenario.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Hi Michael,

Not knowing Java, what's the runnable interface and what does run() do?

I've an idea that asynchronous Delegates might be useful to you, but I'm
not sure.

Regards,
Fergus
 
I'm just getting going on threading and have struggled a bit with it, but in
the course of doing so, I found something that I think might help you. I
think what you need to do is create an abstract (MustInherit) class that
does all the basic stuff for creating a new thread and allowing you to poll
whether the thread is complete. I can't take credit for it, it's from
Francesco Balena's book Programming Microsoft Visual Basic .NET, but I
figured it might help you anyway. Here is the code for Balena's
ThreadWrapperBase class:

Public MustInherit Class ThreadWrapperBase
Private m_Done As Boolean

Public ReadOnly Thread As System.Threading.Thread

Sub New()

Me.Thread = New System.Threading.Thread(AddressOf Me.RunThread)

End Sub

Public Overridable Sub Start()

Me.Thread.Start()

End Sub

Private Sub RunThread()

m_Done = False

OnStart()

m_Done = True

End Sub

Public ReadOnly Property Done() As Boolean

Get

Return m_Done

End Get

End Property

Protected MustOverride Sub OnStart()

End Class


The idea is you can derive from it with classes that override the OnStart
procedure, which kind of simplifies the whole process. Anyway, hope it
helps.

Mike

--


Michael Caputo
Programmer/Database Administrator
Simon Economic Systems Ltd.
847-272-7691
(e-mail address removed)

I am new to vb.net and just playing around to get a feel
for it. I am coming from a vb6/java background so please
excuse me if my idea is plain stupid or contra good
programming practice in dot net. In java a class can
implement runnable and then the run method is started as a
new thread. I want to attempt to do the same thing in
vb.net but have struggled to find any examples about doing
anything like this. I know I could spawn a thread as
normal in a class but was just thinking ( dangerous I am
usually told :-) ) about a more elegant solution.

Regards,

Michael
 
Hello,

Michael said:
I am new to vb.net and just playing around to get a feel
for it. I am coming from a vb6/java background so please
excuse me if my idea is plain stupid or contra good
programming practice in dot net. In java a class can
implement runnable and then the run method is started as a
new thread. I want to attempt to do the same thing in
vb.net but have struggled to find any examples about doing
anything like this. I know I could spawn a thread as
normal in a class but was just thinking ( dangerous I am
usually told :-) ) about a more elegant solution.

Multithreading in Visual Basic .NET (Visual Basic Language Concepts)
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisualbasic.asp
 
This is general response to people's helpful comments. I
had kinda guessed/assumed inheritance was wrong but not
being able to find an equivalent interface to runnable I
posted with the info I had at my finger tips. In slightly
more detail about runnable/run method from Java it allowed
you to create new classes that automatically started a new
thread using executing the code in the run method. Ie no
need for me to repeatedly write thread
starting/initialising code. I think I can approximate the
functionality using Mike Caputo's information about
abstract (must inherit) solution.

The madness/logic behind what I am attempting is that I am
creating a small internal application that is going to
take streams from a couple of webcams and display the
images on one form. I am using a separate thread to grab
images from each camera. I have to write this so that it
would be easy to extend to add more camera's hence,
wanting a class you can create new instances of that would
happily go about their job of grabbing pictures.

Thanks, for the advice. Slowly getting the hang of this
vb.net lark but but my oop knowledge gained from java has
been corrupted by a few years working with plain old vb.
As a result my brain has all sorts of crazy ideas about
how to do things that aren't necessarily the "best" way
forward.
 
Herfried,

Thanks for making me aware of that link. Lots of
interesting reading there! One thing I have picked up on
was the suggestion not to use synclock statement to block
threads when accessing controls. Referring to my general
response to Dick, which explained my small application, I
have threads updating picture boxes on a form. Occasionaly
I wish to save pictures. Therefore to control access to
the picture boxes I am using eg synclock picturebox1 to
make sure the picturebox was in a known stable state. Is
this method likely to cause problems and should I change
the methodolgy to having global variables to synclock
against?

Regards,

Michael
 
Michael,

If you are using a delegate and PictureBox1.Invoke to update the picture box
from your various threads (as you should be doing) then you don't need any
synchronization at all. All your updates are being marshalled to the UI
thread so the picture box is only ever being accessed by one thread.


Herfried,

Thanks for making me aware of that link. Lots of
interesting reading there! One thing I have picked up on
was the suggestion not to use synclock statement to block
threads when accessing controls. Referring to my general
response to Dick, which explained my small application, I
have threads updating picture boxes on a form. Occasionaly
I wish to save pictures. Therefore to control access to
the picture boxes I am using eg synclock picturebox1 to
make sure the picturebox was in a known stable state. Is
this method likely to cause problems and should I change
the methodolgy to having global variables to synclock
against?

Regards,

Michael
 
Hi,

It is fairly standard to create a class that instantiates a thread to do the
work. At least, that is what I normally do. So, your goal not only CAN be
done, but it is commonly done.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Hi Michael,

Your runnable class is pretty much what I guessed.

Here's a class (at end) similar to what Mike gave but it uses Delegates
rather than creating Threads directly. (though I've called the Delegate
variable CameraThread). Delegates can be called asynchronously in hich case
they grab a Thread from the ThreadPool. This is recommended practice where
you'll be using a number of Threads.

The class also uses a ListBox to show feedback and demonstrates the safe
GUI access that Stephen was talking about.

ListBox.BeginInvoke

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/fr
lrfsystemwindowsformscontrolclassbegininvoketopic2.asp

Delegates Tutorial (not as helpful as it could be)
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpovrasynchronousdelegates.asp

Regards,
Fergus

<code>
Public Module RunnableCameras
Public lstCameras As ListBox

Public Sub LetsHaveSomeCameras
Dim aoCameras(5) As Camera
Dim N As Integer
For N = 0 To 4
aoCameras(N) = New Camera ("Camera " & N + 1)
aoCameras(N).Start ("Hi from " & N + 1)
Threading.Thread.Sleep (150) 'Stagger them for fun.
Next
End Sub

Public Class Camera
Delegate Sub CameraThread (sFoo As String)
Delegate Sub FeedbackCallBack (sText As String)

Private oThread As New CameraThread (AddressOf GoCameraGo)
Private sName As String

'This will execute in the caller's thread.
Public Sub New (sThisName As String)
sName = sThisName & " "
End Sub

'This will execute in the caller's thread.
Public Sub Start (sFoo As String)
oThread.BeginInvoke (sFoo, Nothing, Nothing)
End Sub

'This will execute in its own (ThreadPool) thread.
Public Sub GoCameraGo (sFoo As String)
Dim GiveFeedback As New FeedbackCallBack (AddressOf AddToList)
Dim I As Integer

For I = 1 To 50
Dim sBar As String = sName & "[" & I & "]: " & sFoo
'Tell lstCameras via Message Queue to "Come get some!!"
lstCameras.BeginInvoke (GiveFeedback, New Object() {sBar})
Threading.Thread.Sleep (30)
Next
End Sub

'This will execute in the ListBox's thread.
Private Sub AddToList (sText As String)
lstCameras.Items.Add (sText)
lstCameras.SelectedIndex = lstCameras.Items.Count - 1
End Sub
End Class
End Module

Private Sub DoTest
lstCameras = Me.ListBox1
LetsHaveSomeCameras
End Sub
</code>
 
I must thank you for your time in creating that example
code. I will have a play with it once I have a bit more
free time. All this work and other projects does seem to
get in the way :-)

In the haphazard way I am learning about this whole NET
malarky I had not come across delegates. I think it is
about time I bought more structure to my learning and
bought a book. Any suggestions for a decent book for a
spot of reading to gently lull me to sleep?

Regards,

Michael
-----Original Message-----
Hi Michael,

Your runnable class is pretty much what I guessed.

Here's a class (at end) similar to what Mike gave but it uses Delegates
rather than creating Threads directly. (though I've called the Delegate
variable CameraThread). Delegates can be called asynchronously in hich case
they grab a Thread from the ThreadPool. This is recommended practice where
you'll be using a number of Threads.

The class also uses a ListBox to show feedback and demonstrates the safe
GUI access that Stephen was talking about.

ListBox.BeginInvoke

http://msdn.microsoft.com/library/default.asp? url=/library/en-us/cpref/html/fr
lrfsystemwindowsformscontrolclassbegininvoketopic2.asp

Delegates Tutorial (not as helpful as it could be)
http://msdn.microsoft.com/library/default.asp? url=/library/en-
us/cpguide/html/cpovrasynchronousdelegates.asp

Regards,
Fergus

<code>
Public Module RunnableCameras
Public lstCameras As ListBox

Public Sub LetsHaveSomeCameras
Dim aoCameras(5) As Camera
Dim N As Integer
For N = 0 To 4
aoCameras(N) = New Camera ("Camera " & N + 1)
aoCameras(N).Start ("Hi from " & N + 1)
Threading.Thread.Sleep (150) 'Stagger them for fun.
Next
End Sub

Public Class Camera
Delegate Sub CameraThread (sFoo As String)
Delegate Sub FeedbackCallBack (sText As String)

Private oThread As New CameraThread (AddressOf GoCameraGo)
Private sName As String

'This will execute in the caller's thread.
Public Sub New (sThisName As String)
sName = sThisName & " "
End Sub

'This will execute in the caller's thread.
Public Sub Start (sFoo As String)
oThread.BeginInvoke (sFoo, Nothing, Nothing)
End Sub

'This will execute in its own (ThreadPool) thread.
Public Sub GoCameraGo (sFoo As String)
Dim GiveFeedback As New FeedbackCallBack (AddressOf AddToList)
Dim I As Integer

For I = 1 To 50
Dim sBar As String = sName & "[" & I & "]: " & sFoo
'Tell lstCameras via Message Queue to "Come get some!!"
lstCameras.BeginInvoke (GiveFeedback, New Object() {sBar})
Threading.Thread.Sleep (30)
Next
End Sub

'This will execute in the ListBox's thread.
Private Sub AddToList (sText As String)
lstCameras.Items.Add (sText)
lstCameras.SelectedIndex = lstCameras.Items.Count - 1
End Sub
End Class
End Module

Private Sub DoTest
lstCameras = Me.ListBox1
LetsHaveSomeCameras
End Sub
</code>


.
 
Hi Michael,

Although I've not read it, C# for Java Programmers (Syngress) sounds as if
it might suit you.

Regards,
Fergus
 
Back
Top