473,326 Members | 2,076 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,326 software developers and data experts.

Using Sub New in a Class run as a thread

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
Nov 20 '05 #1
3 2947
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/de...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/de...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.

Nov 20 '05 #2
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_Trun kA_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" <v-******@online.microsoft.com> wrote in message
news:8Y****************@cpmsftngxa07.phx.gbl...
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/de...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/de...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.

Nov 20 '05 #3
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/de...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.

Nov 20 '05 #4

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

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
0
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with...
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
11
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
4
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the...
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.