Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:07:07+00:00 2026-06-18T01:07:07+00:00

ALL I was working on a little code which is for search a thread

  • 0

ALL

I was working on a little code which is for search a thread by thread id in the processes of computer.

All my code looks like below , Please help to review it. 🙂

    using System.Diagnostics;

    public class NKDiagnostics
    {
        private Process[] m_arrSysProcesses;

        private void Init()
        {
            m_arrSysProcesses = Process.GetProcesses(".");
        }
        public static ProcessThread[] GetProcessThreads(int nProcID)
        {
            try
            {
                Process proc = Process.GetProcessById(nProcID);
                ProcessThread[] threads = new ProcessThread[proc.Threads.Count];
                proc.Threads.CopyTo(threads, 0);
                return threads;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }

and In another class, I assign a thread to execute my function named DoNothing

    ThreadPool.QueueUserWorkItem((t) => Utility.DoNothing((TimeSpan)t), 
TimeSpan.FromMinutes(1));

and the function DoNothing code is

public class Utility
    {
        public static void DoNothing(TimeSpan timeout, TextBox txtThreadId)
        {
            TimeoutHelper helper = new TimeoutHelper(timeout);
            while (true)
            {
                Thread.Sleep(1000 * 5);
                if (helper.RemainingTime() <= TimeSpan.Zero)
                {
                    MessageBox.Show("This thread's work is finished.");
                    break;
                }
                else
                {

                    if (Thread.CurrentThread.IsThreadPoolThread)
                    {
                        MessageBox.show( Thread.CurrentThread.ManagedThreadId.ToString());
                    }

                }
            }
        }
}

My problem is the Thread.CurrentThread.ManagedThreadId shows 10, I searched it in all the process. But did’t found it .

ProcessThread[] m_Threads = NKDiagnostics.GetProcessThreads(processId);
for (int i = 0; i < m_Threads.Length; i++)
            {

                if (m_Threads[i].Id.Equals(10))
                {
                    MessageBox.Show("Found it.");
                }
}

Am I missing something? why I can’t find this thread ? please help me .thanks.

Updated

The original idea of mine to do some experiment with this code is trying to find a way to get the status of the managed thread. obviously in the way I posted here doesn’t make it. so my question is how can I know the status of managed thread with specified thread id? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:07:08+00:00Added an answer on June 18, 2026 at 1:07 am

    Thread.ManagedThreadId and ProcessThread.Id are not comparable. The first is assigned by the .NET runtime, while the second is the value of the native thread handle the OS assigns to each thread.

    It is also not possible to map one to the other:

    An operating-system ThreadId has no fixed relationship to a managed
    thread, because an unmanaged host can control the relationship between
    managed and unmanaged threads. Specifically, a sophisticated host can
    use the Fiber API to schedule many managed threads against the same
    operating system thread, or to move a managed thread among different
    operating system threads.

    Therefore your code cannot be made to work as is.

    As an aside, there is a possible race condition here:

    ProcessThread[] threads = new ProcessThread[proc.Threads.Count];
    proc.Threads.CopyTo(threads, 0);
    

    It is possible that proc.Threads is modified after the array has been initialized but before CopyTo executes. To avoid this race condition evaluate proc.Threads only once, for example:

    var threads = proc.Threads.ToArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey so I have all my code working and it's fine. But I'd like
I'm working on a little Java game in which all sorts of events can
I have a draggable table which all working fine, though I want to be
I've got the following snippet of code and its all working, and bringing in
I have the following code which is just about working to give the impression
I have a little archer game I'm working on, and previously in my code
I'm working on a little system for SecondLife which involves a bit of heavy
All, I'm working with this Ajax tutorial which basically pull some records from a
I'm currently working on saving larger object structures which contain all the data needed
Here is whats happening (Code Block Below): I am working on a simple drop-down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.