Solved

How to name threads in Visual Studio 2005?

Posted on 2006-07-06
6
244 Views
Last Modified: 2011-10-03
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!
0
Comment
Question by:psmithphil
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 17056112
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

0
 

Author Comment

by:psmithphil
ID: 17057598
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
0
 
LVL 8

Accepted Solution

by:
Shakti109 earned 500 total points
ID: 17058081

Can you describe where your code dosent work if you use the standard "threading.thread" object?
I have a few multi-threaded applications in vb.net 2005, and all of them use this method.

Taking your basic code, and modifying it a little (just running through the subs in the class without logic/etc).

dim newthread as new ConvertTifToPdf
dim wrkthrd as thread

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
    wrkthrd = new thread(addressof newthread.startprocess)
    wrkthrd.name = "Loading thread"
    wrkthrd.Start()
End Sub

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.

public sub StartProcess
    call me.processfiles( di, objStreamWriter )
    call me.logreadcallback
end sub

Public Sub LogReadCallBack()
    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

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:psmithphil
ID: 17058651
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!
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17058832

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!
0
 

Author Comment

by:psmithphil
ID: 17059325
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!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question