Solved

vb.net How can I tell what the current thread is

Posted on 2013-05-13
3
517 Views
Last Modified: 2013-05-14
I am using the following logic to determine what the current thread is but the code is not working.

I should hit the if statement and set the isCurrent text if I find the current thread. What is wrong with my code?

Dim proc As Process = Process.GetCurrentProcess()
            Dim thread As ProcessThread = Nothing
            Dim isCurrent As String = ""

            For Each thread In proc.Threads
                If System.Threading.Thread.CurrentThread Is thread Then
                    isCurrent = "Current_thread: "
                End If
                logmessage(isCurrent & "thread id = " & thread.Id & _
                    " thread.CurrentPriority = " & thread.CurrentPriority)
            Next
0
Comment
Question by:cschene
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39163360
Basically you can't do it that way...

What are you going to with the "current" ProcessThread?

You can use the GetCurrentThreadId() API to get the ID of the current thread:
Public Class Form1

    <Runtime.InteropServices.DllImport("kernel32.dll")> _
    Public Shared Function GetCurrentThreadId() As Integer
    End Function

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim isCurrent As String
        Dim CurrentThreadID As Integer = GetCurrentThreadId()
        For Each PT As ProcessThread In Process.GetCurrentProcess.Threads
            If CurrentThreadID = PT.Id Then
                isCurrent = "Current_thread: "
            Else
                isCurrent = ""
            End If
            logmessage(isCurrent & "thread id = " & PT.Id & _
                " thread.CurrentPriority = " & PT.CurrentPriority)
        Next
    End Sub

    Private Sub logmessage(ByVal str As String)
        Debug.Print(str)
    End Sub

End Class

Open in new window

0
 

Author Comment

by:cschene
ID: 39163475
"What are you going to with the "current" ProcessThread?"

I am just trying to get a thread dump for debugging purposes
0
 

Author Closing Comment

by:cschene
ID: 39164741
Works great. Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

15 Experts available now in Live!

Get 1:1 Help Now