You can use SendMessageTimeout WINAPI function, which receives the message belongs to the same queue as the current thread, the window procedure is called directly—the time-out value is ignored. The below code will help
DWORD result2 = 0;
LRESULT result = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,(LPARAM)_T("Environment"), SMTO_ABORTIFHUNG, 5000, &result2);