Solved

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

Posted on 2013-05-13
3
575 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:Christopher Schene
[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
  • 2
3 Comments
 
LVL 86

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:Christopher Schene
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:Christopher Schene
ID: 39164741
Works great. Thanks!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 64
PowerShell: ForEach ($y in $x.licenses | where {....} 3 55
how to remove error in database 6 36
Copying from a network share 3 26
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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