Solved

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

Posted on 2013-05-13
3
537 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 25
Store results in vb.net 3 20
SqlServer no dupes 25 35
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 35
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

777 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