1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
const int NUMPRIORITIES = 5;
const int OFFSET = 2;
static BOOL suspend1 = FALSE;
static BOOL suspend2 = FALSE;
static int threadPriority1;
static int threadPriority2;
DWORD exitCode1;
DWORD exitCode2;
long i;
TCHAR priorities[NUMPRIORITIES][80] = { TEXT("Lowest"), TEXT("Below Normal"),
TEXT("Normal"), TEXT("Above Normal"),
TEXT("Highest") };
switch (message)
{
case WM_INITDIALOG:
for (i = 0; i < NUMPRIORITIES; i++)
{
SendDlgItemMessage(hDlg, IDC_LB1, LB_ADDSTRING, (WPARAM) 0, (LPARAM) priorities[i]);
SendDlgItemMessage(hDlg, IDC_LB2, LB_ADDSTRING, (WPARAM) 0, (LPARAM) priorities[i]);
}
// get current thread priorities
threadPriority1 = GetThreadPriority(hThread1) + OFFSET;
threadPriority2 = GetThreadPriority(hThread2) + OFFSET;
// update the list boxes
SendDlgItemMessage(hDlg, IDC_LB1, LB_SETCURSEL, (WPARAM) threadPriority1, (LPARAM) 0);
SendDlgItemMessage(hDlg, IDC_LB2, LB_SETCURSEL, (WPARAM) threadPriority2, (LPARAM) 0);
// set suspend and resume buttons for the first thread
if (suspend1 == TRUE)
{
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), TRUE);
}
else
{
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), TRUE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), FALSE);
}
// set suspend and resume buttons for the first thread
if (suspend2 == TRUE)
{
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), TRUE);
}
else
{
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), TRUE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), FALSE);
}
// have both the threads been terminated?
// Then disable a whole lot of buttons
GetExitCodeThread(hThread1, &exitCode1);
GetExitCodeThread(hThread2, &exitCode2);
if ((exitCode1 != STILL_ACTIVE) && (exitCode2 != STILL_ACTIVE))
{
EnableWindow(GetDlgItem(hDlg, IDB_TERMINATE1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_LB1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_TERMINATE2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_LB2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
}
return 1;
case WM_COMMAND:
switch (wParam)
{
case IDB_RESUME1:
ResumeThread(hThread1);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), TRUE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), FALSE);
suspend1 = FALSE;
return 1;
case IDB_SUSPEND1:
SuspendThread(hThread1);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), TRUE);
suspend1 = TRUE;
return 1;
case IDB_TERMINATE1:
TerminateThread(hThread1, 0);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME1), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_LB1), FALSE);
// are both thread dead? If so, disable the Change button
GetExitCodeThread(hThread2, &exitCode2);
if (exitCode2 != STILL_ACTIVE)
{
EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
}
return 1;
case IDB_RESUME2:
ResumeThread(hThread2);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), TRUE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), FALSE);
suspend2 = FALSE;
return 1;
case IDB_SUSPEND2:
SuspendThread(hThread2);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), TRUE);
suspend2 = TRUE;
return 1;
case IDB_TERMINATE2:
TerminateThread(hThread2, 0);
EnableWindow(GetDlgItem(hDlg, IDB_SUSPEND2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDB_RESUME2), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_LB2), FALSE);
// are both thread dead? If so, disable the Change button
GetExitCodeThread(hThread1, &exitCode1);
if (exitCode1 != STILL_ACTIVE)
{
EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
}
return 1;
case IDOK:
// now, actually change the thread priorities
threadPriority1 = (int) SendDlgItemMessage(hDlg, IDC_LB1, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
SetThreadPriority(hThread1, threadPriority1 - OFFSET);
threadPriority2 = (int) SendDlgItemMessage(hDlg, IDC_LB2, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
SetThreadPriority(hThread2, threadPriority2 - OFFSET);
return 1;
case IDCANCEL:
EndDialog(hDlg, 0);
return 1;
}
}
return 0;
}
| |