TerminateThread Return 0 and GetLastError also return 0 RRS feed

  • Question

  • Hi, All

          We develop a GPU win32 application. In this application,some abnormal threads will be generated for unknown reasons and can not finish automatically. The number of threads will be generated more and more,which will affect the start of the normal threads we want to use for time consuming job. After a long time of checking source code, we still don't know how these abnormal threads are generated.  So we decide to kill these abnormal threads forcefully in our application.

           We find that these abnormal threads will be in "DelayExecution" state from start. So we create a timer to check the state of all the threads in this application and find those threads which in the "DelayExecution" state and kill them.  Some of these abnormal threads will be killed successfully, but some CAN NOT be killed with TerminateThread returning 0 and GetLastError return 0.

    The source code of killing abnormal threads is as follows:

                            threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, ThreadID);
                            bExitSuccess = TerminateThread(threadHandle, 0);
                            if (bExitSuccess == 0)
                                qDebug() << "Terminate thread failed: " << GetLastError();
                            bExitSuccess = CloseHandle(threadHandle);
                            if (bExitSuccess == 0)
                                qDebug() << "Close thread failed: " << GetLastError();


    Is there other method to kill the abnormal threads forcefully? Or, how can we debug the TerminateThread to kill the threads successfully?

    Thank you very much!

    Monday, November 30, 2020 4:00 AM

All replies

  • Hi mu kai hui,

    Thank you for posting here.

    Since your application is win32 application, you can consider posting your question on winapi-general forum and provide more descriptions of 'DelayExecution' there.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao

    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    Tuesday, December 1, 2020 5:36 AM
  • OK, Thank you very much!
    Tuesday, December 1, 2020 8:07 AM