?
Solved

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

Posted on 2013-05-13
3
Medium Priority
?
645 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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