Solved

How to name threads in Visual Studio 2005?

Posted on 2006-07-06
6
237 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now