Local Thread

  • Thread starter Thread starter Leon
  • Start date Start date
L

Leon

Is it good ideas to use local thead as following example, then let
..Net garbage Collection take rest, to destory thread, release memory
and other?
Private Function DeleteUser(ByRef UserID As Integer) As Boolean
Dim deleteUser As Thread
Try
If Not IsNothing(DataBase) Then
deleteUser = New Thread(AddressOf Me.DeleteAllUsers)

deleteUser .Start()
End If
Catch ex As Exception

End Try
End Function

Private Function DeleteUser(ByRef UserID As Integer) As Boolean
Dim deleteUser As Thread
Try
If Not IsNothing(DataBase) Then
deleteUser = New Thread(AddressOf Me.DeleteAllUsers )

deleteUser .Start()
End If
Catch ex As Exception
Finally
deleteUser = nothing
End Try
End Function

If I destory the Thread after Finally as bove, does this line of code
always execute after local Thread finish its job first? or it may
destroy the local thread that is still processing?

Thank you very much.
 
If I destory the Thread after Finally as bove, does this line of code
always execute after local Thread finish its job first? or it may
destroy the local thread that is still processing?

Setting your deleteUser reference to Nothing will not destroy
anything, the thread will still run.



Mattias
 
Mattias Sjögren said:
Setting your deleteUser reference to Nothing will not destroy
anything, the thread will still run.



Mattias



So, Do I need worry about memory leak that may be caused by this local
thread? or just trust .net garbage Collection will does good to take
care of it.

Also, if I have follow
Try
Dim mUser As New cUser
mUser.AddUser(user)

Catch ex As Exception
Finally
mUser= Nothing
End Try
inside of AddUser() method, I have a local thread to actually do
adding jobs, does setting mUser = nothing in Finally block this time
has affect? if so, will it destory the local thread that may in
processing?

Thanks
 
A thread goes away, or cleans itself up when it reaches the end of its scope
You don't explicitly need to *do anything. Now if that thread touches
unmanaged stuff then it will be responsible for cleaning up after itself but
you should do that in the main thread routine. It's also a good idea to
error trap for exceptions in thread code because they do go astray
 
So, Do I need worry about memory leak that may be caused by this local
thread? or just trust .net garbage Collection will does good to take
care of it.

No, you don't have to worry about memory leaks here.

What you should worry about is your error handling code.

Catch ex As Exception
Finally

is just plain ugly.

does setting mUser = nothing in Finally block this time
has affect? if so, will it destory the local thread that may in
processing?

No, it will not destroy the thread.



Mattias
 
No, you don't have to worry about memory leaks here.

What you should worry about is your error handling code.

Catch ex As Exception
Finally

is just plain ugly.
It's worse; it'll catch and swallow everything, including fatal exceptions,
like stack overflow, out-of-memory, execution engine exceptions, etc.
 
Back
Top