Using Sub New in a Class run as a thread

B

Bob Day

VS 2003, vb.net, sql msde...

The help is pretty empatic that you cannot pass parameters to a thread. The
sample below is from help, showing you to set up variables in the
TasksClass, and assign their value from sub DoWork where you start the
thread. This approach works, of course.

Why can't you simply add a Sub New to the TaskClass below, and then in sub
DoWork modify the threading line with the sub new arguments needed like
this: Dim Thread1 As New System.Threading.Thread(addressOf
Tasks.SomeTask(subnewarugment1, subnewargument2, etc.) This seems to work
also, and is the way I have been doing it. Is there any down side to this
method?

Thanks!
Bob Day

Class TasksClass

Friend StrArg As String

Friend RetVal As Boolean

Sub SomeTask()

' Use the StrArg field as an argument.

MsgBox("The StrArg contains the string " & StrArg)

RetVal = True ' Set a return value in the return argument.

End Sub

End Class

' To use the class, set the properties or fields that store parameters,

' and then asynchronously call the methods as needed.

Sub DoWork()

Dim Tasks As New TasksClass

Dim Thread1 As New System.Threading.Thread(addressOf Tasks.SomeTask)

Tasks.StrArg = "Some Arg" ' Set a field that is used as an argument

Thread1.Start() ' Start the new thread.

Thread1.Join() ' Wait for thread 1 to finish.

' Display the return value.

MsgBox("Thread 1 returned the value " & Tasks.RetVal)

End Sub
 
P

Peter Huang

Hi Bob,

As far as I know, Thead.New need an ThreadStart delegate, while a
ThreadStart Delegate can not take argument.

Initializes a new instance of the Thread class.
[Visual Basic]
Public Sub New( _
ByVal start As ThreadStart _
)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingthreadclassctortopic.asp

ThreadStart Delegate
Represents the method that executes on the Thread.
[Visual Basic]
<Serializable>
Public Delegate Sub ThreadStart()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingthreadstartclasstopic.asp

Can you show me your code?


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Day

Here are two classes in two different file documents. The first class
FormWin_Status_Of_System_And_Ports creates threads for each telephone line
(each thread is the 2nd class Class_Telephony_Toolkit). The 2nd class has a
sub new, that the 1st class uses to pass arguments. It has worked fine, but
seems contrary to the documentation. The lines of interest are marked by
*****, if you want to search on that.

Thanks!

Bob Day

Friend Class FormWin_Status_Of_System_And_Ports

Inherits System.Windows.Forms.Form

Private Sub Thread_Start_Each_Trunk()

' Purpose: This starts 1 thread per trunk (ie port or talk path). If the
system has 1 card with 4 talk paths (4 RJ 11 modular plugs or 2 RJ14 modular
plugs), will will create 4 threads. These threads stay active until reboot,
in which case they are created again. Each thread contains all of the
telephony methods, like play, get digits, etc. Remember, first Trunk is 0,
last Trunk is 'total -1'


' start at 1st possible trunk which is zero

Dim This_Trunk_0_Based As Integer = 0

' holds last trunk zero based (2 trunks total, 0, 1, would hold 1)

Dim Last_Trunk_0_Based As Integer = -1

' load value

Last_Trunk_0_Based = gsTotal_Useable_Trunks - 1

' hold contents of app.config (true or false)to display CTADE Pop Up Errors

Dim PopUp_Error_Displayed As Boolean

' hold contents of app.config (Enabled, Detailed, Disabled)to CTADE log

Dim Set_Log_Level As String

' get value from app.config

PopUp_Error_Displayed = CBool(App_Config.PopUp_Error_Displayed_Get())

' get value from app.config

Set_Log_Level = App_Config.Set_Log_Level_Get

Do

' create array to hold a thread class instance per trunk

' Instantiate variable Array

Dim Thread_Trunk_Array(This_Trunk_0_Based) As Class_Telephony_Toolkit

' ***** note that the argumens are bing passed via the sub new

' Fill array, note that this instantiates Class

Thread_Trunk_Array(This_Trunk_0_Based) = New
Class_Telephony_Toolkit(This_Trunk_0_Based, PopUp_Error_Displayed, Set_Log_L
evel)

' start the thread

Thread_Trunk_Array(This_Trunk_0_Based).Thread_TrunkA_Start()

' increment up 1 for next trunk (if any)

This_Trunk_0_Based += 1

' repeat for each trunk

Loop Until This_Trunk_0_Based > Last_Trunk_0_Based

' start up stops running here, threads are now running (trunk threads
waiting for call)

End Sub

End Class

----------------------------------------------------------------------------
------------------------

Public Class Class_Telephony_Toolkit

' This class contains all methods, events & Properties available through
Intel's Parity's VoiceBocxLib Telephony Tool kit. Each trunk instantiates
this class to handle that one trunk.

Public Sub New(ByVal This_Trunk_0_Based As Integer, ByVal
CTADE_PopUp_Error_Displayed As Boolean, ByVal CTADE_Set_Log_Level As String)

' *********code to load passed arguments to variables

End Sub

end class

Peter Huang said:
Hi Bob,

As far as I know, Thead.New need an ThreadStart delegate, while a
ThreadStart Delegate can not take argument.

Initializes a new instance of the Thread class.
[Visual Basic]
Public Sub New( _
ByVal start As ThreadStart _
)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingthreadclassctortopic.asp

ThreadStart Delegate
Represents the method that executes on the Thread.
[Visual Basic]
<Serializable>
Public Delegate Sub ThreadStart()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingthreadstartclasstopic.asp

Can you show me your code?


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter Huang

Hi Bob,

Based on your code, it seems that you have an class
Public Class Class_Telephony_Toolkit
you can pass argument to the New keyword of the Class
Class_Telephony_Toolkit.
BTW, since you did not show how you create the thread, I mean I do not know
where you new the Thread instance. I think you should do it in the
Class_Telephony_Toolkit class.

What the doc said is that the New method ( System.Threading.Thread
constructor) will take a ThreadStart as argument, while the ThreadStart
thread proc should be defined as follows.
Public Delegate Sub ThreadStart()

MSDN:
Note Visual Basic users can omit the ThreadStart constructor when
creating a thread. Use the AddressOf operator when passing your method to
the Thread constructor, for example Dim t As New Thread(AddressOf
ThreadProc). Visual Basic automatically calls the ThreadStart constructor.

Based on my understanding , you may try to use the ThreadPool Class
[Visual Basic]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemthreadingthreadpoolclasstopic.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Similar Threads


Top