added PInvoke wait method