Link to home
Start Free TrialLog in
Avatar of psmithphil
psmithphilFlag for United States of America

asked on

How to name threads in Visual Studio 2005?

I have a test program that starts a method (ProcessFiles) onto a new thread.  All works fine, but I want to be able to name the threads.

To make it easy to illustrate, I made a web page that explains the issue:

http://www.geocities.com/psmithphil/ee-issues/expertsexchange-asynchronousmessagingfailsinvs2005.html

I've looked all over the web and can't seem to nail it down.  Can you help?  Thank you!
Avatar of arif_eqbal
arif_eqbal

Sorry I tried opening the link but it didn't
But anyway, if you want to give a Name to a thread just use the Name Property
How are you creating the Thread ? In case you are creating the Threads manually (Not a Thread Pool) then you can easily name it...

Dim Th As New Threading.Thread(AddressOf SomeFunction)
Th.Name="SomeName"
Th.Start

Avatar of psmithphil

ASKER

That's strange the link won't work for you.  I'll test it on some other computers.  Thank you for looking at this.  Here's the text of the web page but unfortunately we can't put pictures in Experts Exchange.  I use "Threading.Thread in VS2003, but it won't work in VS2005.  In VS2005, we have to use a Delegate as I show in my code below.


I’m using Visual Studio 2005 and VB.NET in a Windows application.

I have a test program that starts a method (ProcessFiles) onto a new thread.  All works fine, but I want to be able to name the threads.  I could do this with VS2003 using things like these:

Thread.CurrentThread.Name = "Default Thread"                  ‘For the default thread.
'processingThread.Name = "ProcessFiles Thread"              ‘For the new thread.

But I can’t find how to do it on VS2005.  You can see in the picture below that the default thread (1424) and the new thread (2380) have no name.  I’ve performed a bunch of searching on the web, but to no avail.  I have my code below if that sheds any light.

Imports System
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading      
Imports System.Threading.Thread
Imports System.Runtime.Remoting.Messaging

Public Class ConvertTifToPDF
    'Inherits System.Windows.Forms.Form

'For using Delegate for threading:
Delegate Function BeginThread(ByVal DDi As DirectoryInfo, ByVal Sw As StreamWriter)
Dim StartProcessFiles As BeginThread = New BeginThread(AddressOf ProcessFiles)

Dim Di As DirectoryInfo
Dim objStreamWriter As StreamWriter     'For log file.

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
  Dim aCallBack As AsyncCallback = New AsyncCallback(AddressOf Me.LogReadCallBack)
  Dim Ar As IAsyncResult = StartProcessFiles.BeginInvoke(Di, objStreamWriter, aCallBack, Nothing)
End Sub

Public Sub LogReadCallBack(ByVal Ar As IAsyncResult)
    MessageBox.Show(StartProcessFiles.EndInvoke(Ar))
    MessageBox.Show("In LogReadCallBack Sub")
End Sub

Public Function ProcessFiles(ByVal Di As DirectoryInfo, ByVal sw As StreamWriter)
    Return "Now in ProcessFiles"
End Function
End Class
ASKER CERTIFIED SOLUTION
Avatar of Shakti109
Shakti109

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I appreciate your response!  This is interesting.   If I use "System.Threading.Thread" instead of creating the new instance of the class (ConvertTifToPDF) like you do, then it doesn't work in VS2005 where it used to work in VS2003.  But your way works fine!   I find it interesting that to make it work, I have to create a new instance of a class.  I'm going to have to think about this one, to understand what I am creating by making a new instance of the Class and what issues (if any) may be there by putting it on a new thread.  I also can't see how to name the original thread, where I could do that in VS2003.

I still want to find out how to name the thread when I use delegates so I think I'll submit another question.  However, you've taught me a valuable lesson here and I happily award you the points!  Thank you!

You are quite welcome.

In general, creating a class to hold your thread "logic" etc, allows you to cleanly and clearly maintain the thread routines. It also allows you to implement things like good garbage collection (setting the class to nothing when done with it).

You can create methods/properties in the class to interact with the thread, outside of the normal "thread.abort, thread.stop" / etc that allow you to extend its functionality.

As to naming the "original" thread, this will depend on what it is. If you are using a thread, to call a thread, then the process is similar. I.E : You have a thread-spinner class, that spins out X threads and assigns work (or points them at a queue / etc) and is named in the same manner (threadmaster.name = "MasterThread"), and as it creates the new threads, it names them.

If you are wanting to name the root thread of the main application, that is different.

Good luck, and happy coding!
Thank you, Shakti109, this explanation helps a lot and will assist me in learning more about this (like seeing what a thread-spinner class is and how to use it).  I like the idea that I can control the thread and can then get rid of it when I want by setting the new class instance to nothing (and also creating methods & properties for it).    I find this threading very interesting.  Using Delegates works fine, but with your way it seems I have more control.  

I really appreciate that you not only gave me an answer, but explained the "why".  I don't get such excellent assistance very often, and it's much appreciated!