I discussed yesterday's problem with Anakin in the morning. Although there was no result, I learned how to view the thread from him.
What is the difference between processthread and thread!
The problem is solved.
Yesterday's problem was that threads [0] In process. getcurrentprocess (). threads [0]. userprocessortime was not the current process. Of course, the result is
Not necessarily. In vs2005 IDE, we can find that there are 11 threads, and threads [0] is not the current thread. The key issue is how to obtain the current thread.
In the processthread class, only the property ID is available, and the thread property does not have the ID and only the name is available. Therefore, the current line cannot be obtained through getcurrentthread of the thread class.
Process ID. You can only use other methods to contact processthread. After investigation, it is found that a system function can obtain the ID of the current thread:
[Dllimport ( " Kernel32 " , Entrypoint = " Getcurrentthreadid " , Exactspelling = True )]
Public Static Extern Int32 getcurrentwin32threadid ();
After obtaining the ID of the current thread, you can find the current processthread in the process. getcurrentprocess (). threads set. The function is as follows:
Code
Class Threadutility
{
[Dllimport ( " Kernel32 " , Entrypoint = " Getcurrentthreadid " , Exactspelling = True )]
Public Static Extern Int32 getcurrentwin32threadid ();
Public Static Processthread getprocessthreadfromwin32id (int32 threadid)
{
Processthread pthred = Null ;
If (Threadid = 0 )
Threadid = Threadutility. getcurrentwin32threadid ();
Foreach (Processthread prothread In Process. getcurrentprocess (). threads)
{
If (Prothread. ID = Threadid)
Pthred = Prothread;
Else
Pthred = Process. getcurrentprocess (). threads [ 0 ];
}
Return Pthred;
}
}
In this way, the problem is solved.
In summary, processthread records important information about the thread, such as userprocessortime. However, peocess does not have a ready-made function to directly find the current line.
So you need to use the Win32 system function to find the current thread ID, and then find it in the processthread set.