remove whitespace from end of lines
[reactos.git] / reactos / subsys / system / taskmgr / procpage.c
1 /*
2 * ReactOS Task Manager
3 *
4 * procpage.c
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "precomp.h"
24
25 HWND hProcessPage; /* Process List Property Page */
26
27 HWND hProcessPageListCtrl; /* Process ListCtrl Window */
28 HWND hProcessPageHeaderCtrl; /* Process Header Control */
29 HWND hProcessPageEndProcessButton; /* Process End Process button */
30 HWND hProcessPageShowAllProcessesButton;/* Process Show All Processes checkbox */
31
32 static int nProcessPageWidth;
33 static int nProcessPageHeight;
34
35 static HANDLE hProcessPageEvent = NULL; /* When this event becomes signaled then we refresh the process list */
36
37 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam);
38 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount);
39 void ProcessPageShowContextMenu(DWORD dwProcessId);
40 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter);
41
42 INT_PTR CALLBACK
43 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
44 {
45 RECT rc;
46 int nXDifference;
47 int nYDifference;
48 int cx, cy;
49
50 switch (message) {
51 case WM_INITDIALOG:
52 /*
53 * Save the width and height
54 */
55 GetClientRect(hDlg, &rc);
56 nProcessPageWidth = rc.right;
57 nProcessPageHeight = rc.bottom;
58
59 /* Update window position */
60 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
61
62 /*
63 * Get handles to the controls
64 */
65 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
66 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
67 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
68 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
69
70 /*
71 * Set the font, title, and extended window styles for the list control
72 */
73 SendMessage(hProcessPageListCtrl, WM_SETFONT, SendMessage(hProcessPage, WM_GETFONT, 0, 0), TRUE);
74 SetWindowText(hProcessPageListCtrl, _T("Processes"));
75 ListView_SetExtendedListViewStyle(hProcessPageListCtrl, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
76
77 AddColumns();
78
79 /*
80 * Subclass the process list control so we can intercept WM_ERASEBKGND
81 */
82 OldProcessListWndProc = (WNDPROC)SetWindowLongPtr(hProcessPageListCtrl, GWL_WNDPROC, (DWORD_PTR)ProcessListWndProc);
83
84 /* Start our refresh thread */
85 CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, NULL);
86
87 return TRUE;
88
89 case WM_DESTROY:
90 /* Close the event handle, this will make the */
91 /* refresh thread exit when the wait fails */
92 CloseHandle(hProcessPageEvent);
93
94 SaveColumnSettings();
95
96 break;
97
98 case WM_COMMAND:
99 /* Handle the button clicks */
100 switch (LOWORD(wParam))
101 {
102 case IDC_ENDPROCESS:
103 ProcessPage_OnEndProcess();
104 }
105 break;
106
107 case WM_SIZE:
108 if (wParam == SIZE_MINIMIZED)
109 return 0;
110
111 cx = LOWORD(lParam);
112 cy = HIWORD(lParam);
113 nXDifference = cx - nProcessPageWidth;
114 nYDifference = cy - nProcessPageHeight;
115 nProcessPageWidth = cx;
116 nProcessPageHeight = cy;
117
118 /* Reposition the application page's controls */
119 GetWindowRect(hProcessPageListCtrl, &rc);
120 cx = (rc.right - rc.left) + nXDifference;
121 cy = (rc.bottom - rc.top) + nYDifference;
122 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
123 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
124
125 GetClientRect(hProcessPageEndProcessButton, &rc);
126 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(PRECT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
127 cx = rc.left + nXDifference;
128 cy = rc.top + nYDifference;
129 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
130 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
131
132 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
133 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(PRECT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
134 cx = rc.left;
135 cy = rc.top + nYDifference;
136 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
137 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
138
139 break;
140
141 case WM_NOTIFY:
142
143 ProcessPageOnNotify(wParam, lParam);
144 break;
145 }
146
147 return 0;
148 }
149
150 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
151 {
152 int idctrl;
153 LPNMHDR pnmh;
154 LPNMLISTVIEW pnmv;
155 NMLVDISPINFO* pnmdi;
156 LPNMHEADER pnmhdr;
157 LVITEM lvitem;
158 ULONG Index;
159 ULONG ColumnIndex;
160 IO_COUNTERS iocounters;
161 TIME time;
162
163 idctrl = (int) wParam;
164 pnmh = (LPNMHDR) lParam;
165 pnmv = (LPNMLISTVIEW) lParam;
166 pnmdi = (NMLVDISPINFO*) lParam;
167 pnmhdr = (LPNMHEADER) lParam;
168
169 if (pnmh->hwndFrom == hProcessPageListCtrl)
170 {
171 switch (pnmh->code)
172 {
173 #if 0
174 case LVN_ITEMCHANGED:
175 ProcessPageUpdate();
176 break;
177 #endif
178
179 case LVN_GETDISPINFO:
180
181 if (!(pnmdi->item.mask & LVIF_TEXT))
182 break;
183
184 ColumnIndex = pnmdi->item.iSubItem;
185 Index = pnmdi->item.iItem;
186
187 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
188 PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
189 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
190 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index));
191 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
192 PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax);
193 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
194 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index));
195 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
196 wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index));
197 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
198 {
199 DWORD dwHours;
200 DWORD dwMinutes;
201 DWORD dwSeconds;
202
203 time = PerfDataGetCPUTime(Index);
204 #ifdef _MSC_VER
205 dwHours = (DWORD)(time.QuadPart / 36000000000L);
206 dwMinutes = (DWORD)((time.QuadPart % 36000000000L) / 600000000L);
207 dwSeconds = (DWORD)(((time.QuadPart % 36000000000L) % 600000000L) / 10000000L);
208 #else
209 dwHours = (DWORD)(time.QuadPart / 36000000000LL);
210 dwMinutes = (DWORD)((time.QuadPart % 36000000000LL) / 600000000LL);
211 dwSeconds = (DWORD)(((time.QuadPart % 36000000000LL) % 600000000LL) / 10000000LL);
212 #endif
213 wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds);
214 }
215 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
216 {
217 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024);
218 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
219 _tcscat(pnmdi->item.pszText, _T(" K"));
220 }
221 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
222 {
223 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
224 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
225 _tcscat(pnmdi->item.pszText, _T(" K"));
226 }
227 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
228 {
229 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024);
230 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
231 _tcscat(pnmdi->item.pszText, _T(" K"));
232 }
233 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
234 {
235 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index));
236 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
237 }
238 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
239 {
240 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index));
241 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
242 }
243 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
244 {
245 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
246 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
247 _tcscat(pnmdi->item.pszText, _T(" K"));
248 }
249 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
250 {
251 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024);
252 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
253 _tcscat(pnmdi->item.pszText, _T(" K"));
254 }
255 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
256 {
257 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
258 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
259 _tcscat(pnmdi->item.pszText, _T(" K"));
260 }
261 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
262 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index));
263 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
264 {
265 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index));
266 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
267 }
268 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
269 {
270 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index));
271 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
272 }
273 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
274 {
275 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index));
276 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
277 }
278 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
279 {
280 wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index));
281 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
282 }
283 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
284 {
285 PerfDataGetIOCounters(Index, &iocounters);
286 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */
287 #ifdef UNICODE
288 #define _ui64toa _ui64tow
289 #else
290 #endif
291 _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10);
292 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
293 }
294 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
295 {
296 PerfDataGetIOCounters(Index, &iocounters);
297 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */
298 _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10);
299 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
300 }
301 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
302 {
303 PerfDataGetIOCounters(Index, &iocounters);
304 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */
305 _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10);
306 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
307 }
308 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
309 {
310 PerfDataGetIOCounters(Index, &iocounters);
311 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */
312 _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10);
313 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
314 }
315 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
316 {
317 PerfDataGetIOCounters(Index, &iocounters);
318 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */
319 _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10);
320 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
321 }
322 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
323 {
324 PerfDataGetIOCounters(Index, &iocounters);
325 /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */
326 _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10);
327 CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax);
328 }
329
330 break;
331
332 case NM_RCLICK:
333
334 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
335 {
336 memset(&lvitem, 0, sizeof(LVITEM));
337
338 lvitem.mask = LVIF_STATE;
339 lvitem.stateMask = LVIS_SELECTED;
340 lvitem.iItem = Index;
341
342 ListView_GetItem(hProcessPageListCtrl, &lvitem);
343
344 if (lvitem.state & LVIS_SELECTED)
345 break;
346 }
347
348 if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) &&
349 (PerfDataGetProcessId(Index) != 0))
350 {
351 ProcessPageShowContextMenu(PerfDataGetProcessId(Index));
352 }
353
354 break;
355
356 }
357 }
358 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
359 {
360 switch (pnmh->code)
361 {
362 case HDN_ITEMCLICK:
363
364 /*
365 * FIXME: Fix the column sorting
366 *
367 *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL);
368 *bSortAscending = !bSortAscending;
369 */
370
371 break;
372
373 case HDN_ITEMCHANGED:
374
375 UpdateColumnDataHints();
376
377 break;
378
379 case HDN_ENDDRAG:
380
381 UpdateColumnDataHints();
382
383 break;
384
385 }
386 }
387
388 }
389
390 void CommaSeparateNumberString(LPTSTR strNumber, int nMaxCount)
391 {
392 TCHAR temp[260];
393 UINT i, j, k;
394
395 for (i=0,j=0; i<(_tcslen(strNumber) % 3); i++, j++)
396 temp[j] = strNumber[i];
397 for (k=0; i<_tcslen(strNumber); i++,j++,k++) {
398 if ((k % 3 == 0) && (j > 0))
399 temp[j++] = _T(',');
400 temp[j] = strNumber[i];
401 }
402 temp[j] = _T('\0');
403 _tcsncpy(strNumber, temp, nMaxCount);
404 }
405
406 void ProcessPageShowContextMenu(DWORD dwProcessId)
407 {
408 HMENU hMenu;
409 HMENU hSubMenu;
410 HMENU hPriorityMenu;
411 POINT pt;
412 SYSTEM_INFO si;
413 HANDLE hProcess;
414 DWORD dwProcessPriorityClass;
415 TCHAR strDebugger[260];
416 DWORD dwDebuggerSize;
417 HKEY hKey;
418 UINT Idx;
419
420 memset(&si, 0, sizeof(SYSTEM_INFO));
421
422 GetCursorPos(&pt);
423 GetSystemInfo(&si);
424
425 hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_PROCESS_PAGE_CONTEXT));
426 hSubMenu = GetSubMenu(hMenu, 0);
427 hPriorityMenu = GetSubMenu(hSubMenu, 4);
428
429 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
430 dwProcessPriorityClass = GetPriorityClass(hProcess);
431 CloseHandle(hProcess);
432
433 if (si.dwNumberOfProcessors < 2)
434 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
435
436 if (!DebugChannelsAreSupported())
437 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
438
439 switch (dwProcessPriorityClass) {
440 case REALTIME_PRIORITY_CLASS:
441 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
442 break;
443 case HIGH_PRIORITY_CLASS:
444 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
445 break;
446 case ABOVE_NORMAL_PRIORITY_CLASS:
447 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
448 break;
449 case NORMAL_PRIORITY_CLASS:
450 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
451 break;
452 case BELOW_NORMAL_PRIORITY_CLASS:
453 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
454 break;
455 case IDLE_PRIORITY_CLASS:
456 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
457 break;
458 }
459
460 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
461 {
462 dwDebuggerSize = 260;
463 if (RegQueryValueEx(hKey, _T("Debugger"), NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
464 {
465 for (Idx=0; Idx<_tcslen(strDebugger); Idx++)
466 strDebugger[Idx] = toupper(strDebugger[Idx]);
467
468 if (_tcsstr(strDebugger, _T("DRWTSN32")))
469 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
470 }
471 else
472 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
473
474 RegCloseKey(hKey);
475 } else {
476 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
477 }
478 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
479 DestroyMenu(hMenu);
480 }
481
482 void RefreshProcessPage(void)
483 {
484 /* Signal the event so that our refresh thread */
485 /* will wake up and refresh the process page */
486 SetEvent(hProcessPageEvent);
487 }
488
489 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
490 {
491 ULONG OldProcessorUsage = 0;
492 ULONG OldProcessCount = 0;
493 TCHAR szCpuUsage[256], szProcesses[256];
494
495 /* Create the event */
496 hProcessPageEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
497
498 /* If we couldn't create the event then exit the thread */
499 if (!hProcessPageEvent)
500 return 0;
501
502 LoadString(hInst, IDS_STATUS_CPUUSAGE, szCpuUsage, 256);
503 LoadString(hInst, IDS_STATUS_PROCESSES, szProcesses, 256);
504
505 while (1) {
506 DWORD dwWaitVal;
507
508 /* Wait on the event */
509 dwWaitVal = WaitForSingleObject(hProcessPageEvent, INFINITE);
510
511 /* If the wait failed then the event object must have been */
512 /* closed and the task manager is exiting so exit this thread */
513 if (dwWaitVal == WAIT_FAILED)
514 return 0;
515
516 if (dwWaitVal == WAIT_OBJECT_0) {
517 TCHAR text[260];
518
519 /* Reset our event */
520 ResetEvent(hProcessPageEvent);
521
522 if ((ULONG)SendMessage(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0) != PerfDataGetProcessCount())
523 SendMessage(hProcessPageListCtrl, LVM_SETITEMCOUNT, PerfDataGetProcessCount(), /*LVSICF_NOINVALIDATEALL|*/LVSICF_NOSCROLL);
524
525 if (IsWindowVisible(hProcessPage))
526 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
527
528 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
529 OldProcessorUsage = PerfDataGetProcessorUsage();
530 wsprintf(text, szCpuUsage, OldProcessorUsage);
531 SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
532 }
533 if (OldProcessCount != PerfDataGetProcessCount()) {
534 OldProcessCount = PerfDataGetProcessCount();
535 wsprintf(text, szProcesses, OldProcessCount);
536 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
537 }
538 }
539 }
540 return 0;
541 }