Use GDISelectPalette16 instead of SelectPalette and GDIRealizePalette16 instead of RealizePalette in order to get rid of possible deadlocks.