[Win32 API] PlaySound() delay.

I'm trying to play the default windows ding sound when the user clicks exit or X(close). I've copied the .wave file to my project directory and added the PlaySound() function in the appropriate place. However at the same time that it plays that sound it also pops up a dialog box (illustrated in the code) but the problem I am seeing is that when I click Exit it plays the sound but then waits about 1 - 1.5 seconds or so(noticeable delay) before proceeding to the dialog box. Any fix for this delay? Anyway to make them happen together?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case WM_COMMAND:
{
	switch (LOWORD(wParam))
	{
	case ID_FILE_EXIT:
		EnableWindow(hWnd, FALSE);
		PlaySound(TEXT("Ding.wav"), NULL, SND_SYNC);
		if (MessageBoxA(NULL, "Are you sure you want to exit?", "Exit Confirmation", MB_YESNO) == IDYES)
		{
			PostQuitMessage(WM_QUIT);
			return(0);
		}
		else
		{
			EnableWindow(hWnd, TRUE);
			SetFocus(hWnd);
		} break;
Last edited on
You're using SND_SYNC which plays synchronously (blocking). This means PlaySound will not return (meaning your code pauses) until the sound completes.

You want SND_ASYNC, which plays asynchronously (non-blocking). This means PlaySound will return immediately.
Indeed that was the issue. Should have read a little more on PlaySound(). Appreciate the answer.
Topic archived. No new replies allowed.