Process. getcurrentprocess (). threads set to find the current processthread

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.