Enumerating Threads

< BACK  NEXT >
[oR]

The toolhelp functions can be used to list all the threads running on a device. The code in Listing 5.12 takes a snapshot of all threads by calling CreateToolhelp32Snapshot and passing the TH32CS_SNAPTHREAD constant. The second parameter is a process identifier, and 0 specifies that threads for all processes should be enumerated. The functions Thread32First and Thread32Next are used to enumerate the threads in the snapshot, and data on each thread is placed in a THREADENTRY32 structure. As usual, CloseToolhelp32Snapshot should be called to close the snapshot.

Listing 5.12 Lists running threads
 void Listing5_12() {   HANDLE hThreadSnap;   THREADENTRY32 th32;   hThreadSnap =     CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);   if (hThreadSnap == (HANDLE)-1)   {     cout   _T("Could not take Toolhelp snapshot")            endl;     return ;   }   th32.dwSize = sizeof(THREADENTRY32);   if (Thread32First(hThreadSnap, &th32))   {     do     {       cout   _T("ThreadID: ")              th32.th32ThreadID              _T(" ProcessID: ")               th32.th32OwnerProcessID              _T(" Priority: ")              th32.tpBasePri   endl;     }     while (Thread32Next(hThreadSnap, &th32));   }   CloseToolhelp32Snapshot(hThreadSnap);   return; } 

There are really only three members of THREADENTRY32 that provide useful information:

  • th32ThreadID The thread identifier for the thread

  • th32OwnerProcessID The identifier for the process in which the thread runs

  • tpBasePri The thread's priority as a value between 0 and 255

Running Listing5_12 on a Pocket PC shows that most threads run at priority 251 (THREAD_PRIORITY_NORMAL), others at 255 (THREAD_PRIORITY_ IDLE) and 249 (THREAD_PRIORITY_HIGHEST), and around 10 running at real time priorities such as 109, 132, 118 120 and 126.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net