如何在c#中获取其他进程的当前线程 [英] How to get the Current Thread of other process in c#

查看:453
本文介绍了如何在c#中获取其他进程的当前线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





考虑一下,我正在运行两个应用程序(EXE)E1和E2,并希望知道其他应用程序(E1)的当前线程ID (E2)。



有没有办法实现这个目标?



我能够得到E1和线程在其中运行的过程。





Hi,

Consider, I am running two Applications(EXEs) E1 and E2 and want to know the current thread ID of one application(E1) from other(E2).

Is there any way to achieve this?

I am able to get the process of E1 and threads running inside it.


foreach (Process p in processes)
{
    if (p.ProcessName.Equals(processName))
    {
        handle = p.MainWindowHandle;
        id = (uint)p.Id;
        break;
    }
}





但是怎样

才能准确找到当前的线程ID ?



如果你可以帮我提供代码片段会更有帮助。



先谢谢。



Javithkhan



But how
to find the current thread ID exactly?

It would be more helpful if you could help me with code snippets.

Thanks in Advance.

Javithkhan

推荐答案

如果你仔细想想,你会明白这个问题毫无意义。根据定义,当前线程是调用线程的线程。您在尝试进行某些查询的线程中运行一些代码,并且运行此代码的线程是唯一的当前线程。另一个进程中唯一的线程总是非当前的。



如果你解释一下你想用外部进程的某个线程做什么,我会尝试救命。您可能需要确定该应用程序的启动线程的ID,即UI线程。但是您需要了解进程彼此隔离,在单独的地址空间中执行,但是线程ID在整个操作系统中共享。你几乎无法访问外部线程。



-SA
If you think about it, you will understand that the question makes no sense. By definition, "current thread" is the thread of the calling thread. You run some code in the thread you are trying to do some inquiry, and that thread running this code is the only current thread. The only thread in another process is always non-current.

If you explain what did you want to do with some thread of the external process, I'll try to help. It's possible that you need to determine the ID of the start-up thread of that application, a UI thread. But you need to understand that processes are well isolated from each other, are executed in separate address spaces, but thread ID are shared throughout the OS. You hardly can access much on the external thread.

—SA


这篇关于如何在c#中获取其他进程的当前线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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