Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

14 Experts available now in Live!

Get 1:1 Help Now