void main() { int sum, list[3]; printf("Enter three integers separated by spaces:\n"); scanf("%d%d%d", &list[0], &list[1], &list[2]); sum = AddThree(list); printf("The sum of the three integers is: %d\n", sum); exit(0); }
// Main entry point int __cdecl wmain() { HRESULT hr = S_OK; // Some numbers to add asynchronously // Add has the behavior that if the second parameter is 0, it will perform synchronously int ints[] = { 1, 0, 0, // First add sync, second add sync 2, 1, 0, // First add async, second add sync 3, 0, 2, // First add sync, second add async 4, 3, 2, // First add async, second add async }; // Set up the event that will get signalled each time AddThree is complete HANDLE handle = CreateEvent(NULL, FALSE, FALSE, NULL); if (handle == NULL) { goto Exit; } // Set up the callback to use when performing the addition asynchronously WS_ASYNC_CONTEXT addThreeComplete; addThreeComplete.callback = AddThreeComplete; addThreeComplete.callbackState = handle; // Declare private data for AddThree ADD_STATE addState; // Perform the additions synchronously and asynchronously for (ULONG loop = 0; loop < 2; loop++) { // Add sets of integers that will cause different execution behavior when added asynchronously for (ULONG i = 0; i < sizeof(ints) / sizeof(int); i += 3) { wprintf(L"Adding %d,%d,%d %s...\n", ints[i], ints[i + 1], ints[i + 2], (loop == 0 ? L"synchronously" : L"asynchronously")); // Set up how the function will be called WS_ASYNC_CONTEXT* asyncContext; if (loop == 0) { // Perform the addition synchronously asyncContext = NULL; } else { // Perform the addition asynchronously asyncContext = &addThreeComplete; } // Perform the addition int sum; hr = AddThree(&addState, ints[i], ints[i + 1], ints[i + 2], &sum, asyncContext, NULL); if (hr == WS_S_ASYNC) { // If the operation is being performed asynchronously, then wait for it to complete WaitForSingleObject(handle, INFINITE); } if (SUCCEEDED(hr)) { wprintf(L"Result: %d\n", sum); } else { wprintf(L"AddThree failed.\n"); } } } Exit: if (handle != NULL) { CloseHandle(handle); } fflush(stdout); return SUCCEEDED(hr) ? 0 : -1; }