Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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.


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


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
Posted
Comments
Fredrik Bornander 22-Sep-14 10:38am    
What do you mean by "current thread" in this context?
If the E1 process has several active threads, which one would you consider to be the current one?
Javith khan 22-Sep-14 11:25am    
Hi,

Consider my Application is freezed/hangs due to some exception occured in any of the threads in that application/process.
In this scenario, My Application does not respond and will not be able to proceed further.
Hence, i want to know the current thread at this moment (or) in which thread that exception has occured.

Please feel free if you require any information on the same.

Thanks.
Fredrik Bornander 22-Sep-14 11:48am    
An uncaught exception in a thread will not hang/freeze your application, it will kill the thread and likely the application as well.

If you ask me, "current thread" is only relevant from within that thread. It makes no sense for an outside observer.
Consider a deadlock where you have two treads A and B waiting for each other, which of these is the current one?

I think you're better off looking into hang and crash dumps, and how to generate them. But that's just my $0.02.
Sergey Alexandrovich Kryukov 22-Sep-14 12:00pm    
Why? Could you explain your ultimate goal? Is your purpose just the debugging?
—SA
Javith khan 22-Sep-14 12:36pm    
Hi,
I would like to develop the sample application to generate the Dumpfiles for any application(in which exception occured).

http://blogs.msdn.com/b/dondu/archive/2010/10/24/writing-minidumps-in-c.aspx

I need to know the following values to generate the dumpFile for any process which encountered with exceptions.

currentProcess.Handle,
currentProcess.Id,
currentThread.Id

The first two values, i received using the below snippet.
foreach (Process p in processes)
{
if (p.ProcessName.Equals(processName))
{
handle = p.MainWindowHandle;
id = (uint)p.Id;
break;
}
}

whereas, I am not sure from where i can get the value for currentThread.Id..

In the above blog, they have created the dump file for that particular sample application. Hence they will be able to retrieve all the values required.
But in my case, i am trying to capture all these values for some other application.

Thanks.

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900