Another multi-threading questions about object creation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I found some code on the web that demonstrated using Threading.SynchronizationContext and delegates to handle raising events back to the gui for updating controls and passing data to the worker sub. It works fine but I have some questions about what is really
going on.
Im using Threading.ThreadPool.QueueUserWorkItem to start the thread (within the class that is doing the work).
On my main form I have one instance of the class (m_test). I start the threads in a while loop like this (in main form)


<pre class="prettyprint lang-vb Private WithEvents m_test As New ThreadTest While m_ThreadCount < MAX_THREAD_COUNT m_test.StartAsync(PatientName,ThreadName)
End while [/code]
My Thread code is below.
My question is this: How many objects of of type ThreadTest do I really have going on? Is it just the one that I declared or is each thread creating its own instance of ThreadTest to do the work? If the latter, what is the lifespan of ThreadTest? Does is
go to garbage collection when the thread finishes?
I ask because watching task manager while the app runs continously, the memory slowly creeps up. Thread count stays about the same (Im assuming because of thread pooling).
Thanks for your help.
Thread Code:

<pre class="prettyprint lang-vb Imports System.Threading

Public Class ThreadTest

Private context As Threading.SynchronizationContext = Threading.SynchronizationContext.Current
Private cancelThread As Boolean = False

Public Event ThreadFinished As EventHandler(Of ThreadCompletedEventArgs)
Public Event UpdateGuiFromThread As EventHandler(Of UpdateGuiEventArgs)

Updates thread count in gui
Protected Overridable Sub OnThreadFinished(ByVal e As ThreadCompletedEventArgs)
RaiseEvent ThreadFinished(Me, e)
End Sub

Updates Gui controls
Protected Overridable Sub OnUpdateGui(e As UpdateGuiEventArgs)
RaiseEvent UpdateGuiFromThread(Me, e)
End Sub

Tells thread to exit
Public Sub AbortThread()
cancelThread = True
End Sub

Starts the thread
Public Sub StartAsync(Org As String, id As String)
ThreadExtensions1.QueueUserWorkItem(New Func(Of String, String, String)(AddressOf TestData), Org, id)
End Sub

Does the work
Public Function TestData(V As String, ID As String) As String
Dim msg As String = ""
Dim RandomClass As New Random()
Dim RandomNumber As Integer
Try

RandomNumber = RandomClass.Next(1, 30)

Threading.Thread.Sleep(RandomNumber * 500)
Dim e As New ThreadCompletedEventArgs("Thread Finished", V, ID)

If context Is Nothing Then
OnThreadFinished(e)
Else
ThreadExtensions1.ScSend(context, New Action(Of ThreadCompletedEventArgs)(AddressOf OnThreadFinished), e)
End If

Catch ex As Exception
Dim e As New ThreadCompletedEventArgs("Thread Aborted", V, ID)
OnThreadFinished(e)
End Try

Return (msg)
End Function

End Class




Extends Thread so we can pass arguments of type ThreadCompletedEventArgs and UpdateGuiEventArgs
Public Class ThreadExtensions1
Private args() As Object
Private DelegateToInvoke As [Delegate]

Public Shared Function QueueUserWorkItem(ByVal method As [Delegate], ByVal ParamArray args() As Object) As Boolean
Return Threading.ThreadPool.QueueUserWorkItem(AddressOf ProperDelegate, New ThreadExtensions1 With {.args = args, .DelegateToInvoke = method})
End Function

Public Shared Sub ScSend(ByVal sc As Threading.SynchronizationContext, ByVal del As [Delegate], ByVal ParamArray args() As Object)
sc.Send(New Threading.SendOrPostCallback(AddressOf ProperDelegate), New ThreadExtensions1 With {.args = args, .DelegateToInvoke = del})
End Sub

Private Shared Sub ProperDelegate(ByVal state As Object)
Dim sd1 As ThreadExtensions1 = DirectCast(state, ThreadExtensions1)
sd1.DelegateToInvoke.DynamicInvoke(sd1.args)
End Sub
End Class


#Region "Thread Communications"

Public Class ThreadCompletedEventArgs
Inherits EventArgs

Private _message As String
Private _org As String
Private _threadId As String

Public ReadOnly Property Message() As String
Get
Return (_message)
End Get
End Property

Public ReadOnly Property Org As String
Get
Return (_org)
End Get
End Property

Public ReadOnly Property TheadID As String
Get
Return (_threadId)
End Get
End Property

Public Sub New(msg As String, org As String, id As String)
_message = msg
_org = org
_threadId = id
End Sub
End Class




Public Class UpdateGuiEventArgs
Inherits EventArgs

Private _message As String
Private _threadId As String

Public ReadOnly Property ThreadID As String
Get
Return (_threadId)
End Get
End Property

Public ReadOnly Property Message As String
Get
Return (_message)
End Get
End Property


Public Sub New(msg As String, id As String)
_message = msg
_threadId = id
End Sub
End Class
#End Region[/code]
<br/>



<hr class="sig Mike Rutledge

View the full article
 
Back
Top