Add a Lengthy Operation

A lengthy operation can take many forms. This sample is a function named sieve that computes several hundred prime numbers. The operation begins when the user presses ENTER. Add the following statements to the window function:

case WM_CHAR:

if(wParam == '\r') {

SetCapture(hWnd);

/* Set the cursor to an hourglass */

hSaveCursor = SetCursor(hHourGlass);

strcpy(str, “Calculating prime numbers...”);

InvalidateRect(hWnd, NULL, TRUE);

UpdateWindow(hWnd);

sprintf(str, “Calculated %d primes. ”, sieve());

InvalidateRect(hWnd, NULL, TRUE);

UpdateWindow(hWnd);

SetCursor(hSaveCursor); /* Restores previous cursor */

ReleaseCapture();

}

break;

When the user presses ENTER, Windows generates a WM_CHAR message whose wParam parameter contains an ANSI value representing the carriage return. When the window function receives a WM_CHAR message, it checks for this value and carries out the sample lengthy operation, sieve. This function, called

Eratosthenes Sieve Prime-Number Program, is from Byte, January 1983. It is defined as follows:

#define NITER 20

#define SIZE 8190

char flags[SIZE+1] = { 0};

sieve() {

int i,k;

int iter, count;

for(iter = 1; iter <= NITER; iter++) {

count = 0;

for(i = 0; i <= SIZE; i++)

flags[i] = TRUE;

for(i = 2; i <= SIZE; i++) {

if(flags[i] ) {

for(k = i + i; k <= SIZE; k += i)

flags[k] = FALSE;

count++;

}

}

}

return(count);

}