日期:2014-05-18  浏览次数:21242 次

如何终止指定名称的线程?
有一个多线程应用,用户接入,则开一个线程处理,在用户开线程的过程中,可以设置线程名,问题是,服务器端如何关闭指定名称的线程?

问题2:在服务器端的程序中,假设为某个接入用户开启的线程名是newthread.Name = "aaaaaaa"; //线程名
我在另一个程序中,对服务器进行线程查询,确不能得到线程名!代码如下:
  Process[] allproc = Process.GetProcesses(); //得到系统进程信息
  foreach (Process proc in allproc)
  {
  if (proc.ProcessName.ToLower().CompareTo(sss) == 0)
  {
  ProcessThreadCollection mythreads = proc.Threads;
  foreach (ProcessThread pt in mythreads)
  {
   
  ThreadState ts = pt.ThreadState;
  listBox1.Items.Add("线程ID:"+pt.Id.ToString());
  listBox1.Items.Add("线程状态:"+ts.ToString());
  listBox1.Items.Add(" ");
  }
  }
  }
在pt中,没有线程名的属性,有线程ID,但是我不能得到线程ID和线程名的对应关系,所以我不能关闭线程
如何得到他们的对应关系?
我现在要实现的功能是:如果某个用户长时间不与服务器交互,服务器要踢他下线,问题是不不知道服务器端的N个线程哪个与之对应,无法关闭线程,我必须要得到线程名与用户的对应关系,之后,关闭指定名称的线程!!!!

------解决方案--------------------
试下在线程中附加一些标识来起到关键字的作用,这样的方法不是很“官方”,但我经常这样做。
------解决方案--------------------
C# code

此时,只有执行 Main 的主线程还存在。它会显示一条最终消息,然后返回,从而使主线程也终止。

下面显示了完整的示例:

using System;
using System.Threading;

public class Worker
{
    // This method will be called when the thread is started.
    public void DoWork()
    {
        while (!_shouldStop)
        {
            Console.WriteLine("worker thread: working...");
        }
        Console.WriteLine("worker thread: terminating gracefully.");
    }
    public void RequestStop()
    {
        _shouldStop = true;
    }
    // Volatile is used as hint to the compiler that this data
    // member will be accessed by multiple threads.
    private volatile bool _shouldStop;
}

public class WorkerThreadExample
{
    static void Main()
    {
        // Create the thread object. This does not start the thread.
        Worker workerObject = new Worker();
        Thread workerThread = new Thread(workerObject.DoWork);

        // Start the worker thread.
        workerThread.Start();
        Console.WriteLine("main thread: Starting worker thread...");

        // Loop until worker thread activates.
        while (!workerThread.IsAlive);

        // Put the main thread to sleep for 1 millisecond to
        // allow the worker thread to do some work:
        Thread.Sleep(1);

        // Request that the worker thread stop itself:
        workerObject.RequestStop();

        // Use the Join method to block the current thread 
        // until the object's thread terminates.
        workerThread.Join();
        Console.WriteLine("main thread: Worker thread has terminated.");
    }
}

------解决方案--------------------
线程new后有个ManagedThreadId,可以用来区分,不过注意因为是托管线程,和本地线程是不一样的,是一对多的关系

It is not possible to use ManagedThreadId in this context. This is a completely managed concept and has no real representation in the native world. Hence it doesn't make any sense to the API's you're passing it to. 

The reason ManagedThreadId exists is because there is not necessarily a 1-1 mapping between a native and managed thread. The CLR is free to use multiple native threads to run a single managed thread as long as the native thread is compatible with the one it's replacing. It cannot for instance, be in a different COM apartment. 

In some ways you're a bit stuck here. AFAIK, there is no way to 100% guarantee that you will have the same native thread for a given managed thread. You can achieve a very high level of guarante