全て
私は、コンピューターのプロセスでスレッド ID でスレッドを検索するための小さなコードに取り組んでいました。
私のコードはすべて以下のようになります。レビューにご協力ください。:)
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;
}
}
}
別のクラスでは、次の名前の関数を実行するスレッドを割り当てますDoNothing
ThreadPool.QueueUserWorkItem((t) => Utility.DoNothing((TimeSpan)t),
TimeSpan.FromMinutes(1));
関数DoNothing
コードは
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());
}
}
}
}
}
私の問題はThread.CurrentThread.ManagedThreadId
ショー10です。すべてのプロセスで検索しました。しかし、見つかりませんでした。
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.");
}
}
何か不足していますか?このスレッドが見つからないのはなぜですか? 私を助けてください。ありがとう。
更新しました
このコードでいくつかの実験を行うという私の最初のアイデアは、マネージ スレッドのステータスを取得する方法を見つけようとすることです。明らかに、私がここに投稿した方法ではうまくいきません。私の質問は、指定されたスレッド ID を持つマネージド スレッドのステータスを知るにはどうすればよいですか? ありがとう。