线程编程中的关键问题. [英] critical issue from Thread Programing.

查看:96
本文介绍了线程编程中的关键问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些小项目来管理我创建的线程列表.但是问题是,当我通过
查询它时,我没有办法确定哪个线程属于我的线程 诊断调用(请参见下面的代码).

申请号1

I am doing small procject for managing my created thread list. But the problem is that I don''t have a way to identify which thread is mine when I query it through
the diagnostics call (see code below).

Application Number 1

while(true)
{
  Console.WriteLine(Process.GetCurrentProcess().Id); 
  MyClass w = new MyClass();
  Thread workerThreadA = new Thread(new ThreadStart(w.DoSomeWork));
  Random s = new Random();                 
  workerThreadA.Name = s.Next().ToString() +"_Vijayr";
  workerThreadA.Start(); 
  Thread.Sleep(10000);                 
  Console.WriteLine(Process.GetCurrentProcess().Id);
 }



例如:在应用程序上方创建processId = 5516

申请号2




For Eg :Above Application creating processId = 5516

Application Number 2


Process[] proc = Process.GetProcesses();
foreach (Process process in proc)
{
  if (process.Id.ToString() == "5516")
  {
    ProcessThreadCollection processThread = process.Threads;
    foreach (ProcessThread prcthread in processThread)
    {
      Console.WriteLine("my match with this rocess");                   
      Console.WriteLine(prcthread.ThreadState.ToString()); 
    }
  }
}



我怎么知道线程列表是由进程5516(应用程序1)创建的
因为Thread没有ID,但有一个名称.
ProcessThread具有ID,但没有名称.

如果我尝试在应用程序2中强制转换为Thread thr = (processThread)prcthread,但出现错误,则无法强制转换

查询子胎面时如何识别我创建的线程?

这是我的大部分问题,因为我无法弄清楚
线程和ProcessThread之间的关系.你会以为我会
每个线程都有一个线程ID,以便我识别它.线程ID可以
也很棘手,因为它们被重复使用.线程完成后,
另一个启动时,新线程可能会获得先前运行的线程的ID,
完全的.这使问题更加复杂.

任何人对此都有任何线索.请帮助我找到监视线程的方法

谢谢,

Vijay r



how can i come to know the list of thread are created by process 5516(Application 1)
because Thread has no ID, but has a name.
ProcessThread has an ID but no name.

if i try to cast into Thread thr = (processThread)prcthread in Application 2 ,but i getting error unable to cast

How do I identify my threads I created when I query the child treads?

This is my a large part of my problem is that I can''t figure out the
relationship between Thread and ProcessThread. You would think that I would
have a thread ID in each one so that I could Identify it. Thread IDs can
also be tricky because they are re-used. When a thread completes and
another starts the new thread may get the ID of a previously run thread that
completed. This further complicates the issue.

Any body have any clue on this .please help me to find the monitoring the Thread

Thanks,

Vijay r

推荐答案

好,您可以获取当前进程正在运行的线程的列表,然后对列表进行操作:

Well, you can get a list of the current process'' running threads, and then do something with the list:

using System.Diagnostics;

ProcessThreadCollection threadlist = theProcess.Threads;

foreach(ProcessThread thread in threadlist)
{
    // do something with the thread, thread.Id, thread.PriorityLevel, 
    // thread.StartTime, tread.Name, etc
}


.net Thread类与Win32 Thread不同,后者是ProcessThread类所代表的.与一个ProcessThread关联的线程可能不止一个.我认为您无法使用列出的方法来完成此任务.您可能必须在过程中使用远程处理或类似方法来创建自定义功能,以获取代表线程信息的线程名称和对象.

这是一个可能使您入门的链接:

http://stackoverflow.com/questions/1825882/在c-net中获取当前活动的托管线程列表 [
The .net Thread class is not the same as a Win32 Thread, which is what the ProcessThread class represents. There may be more than one Thread associated with a ProcessThread. I don''t think you can use the approach you listed for this task. You would probably have to create custom functionality from within the process using remoting or something similar to get the thread names and objects representing thread info.

Here''s a link that may get you started:

http://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-c-net[^]


这篇关于线程编程中的关键问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆