if (!....){ DbgPrint ... goto error; } ... error: if (pingthrd) { TerminateThread(pingthrd,0); CloseHandle(pingthrd); }