Yes, you can post a message to the main window from your threads, just before the last line (which has to be a simple return 0, no ExitThread needed).

From you main window procedure, get the message and call CloseHandle. Finally, get WM_DESTROY and call WaitForMultipleObjects on the thread handles that haven't expired yet (and/or send them a message to notify about program termination).

If you don't want to worry about thread termination, just close their handle as soon as you create them (as Krishnaa has already suggested).