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 threadsvoid 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:
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.
|