Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to name threads in Visual Studio 2005?

Posted on 2006-07-06
6
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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