Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExitThread 1

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
i am using following code to exit the thread.
i am calling dll through one exe. on running the exe, one dialog box will appear and user has to click ok to call the dll. The parameters will be passed to the dll and dll will create 3 to 4 thread.On failure of any threads execution i will kill all the threads. the problem is it is killing the dialog thread also. the code is:

creating the threads in loop.
for(IThreadCount=0;IThreadCount<IThreads;IThreadCount++)
{
hThreadHandle[IThreadCount] = CreateThread(NULL,0,run_thread,(LPVOID)&info[IThreadCount],0,&dwThreadId[IThreadCount]);
SetThreadPriority(hThreadHandle[IThreadCount],THREAD_PRIORITY_HIGHEST);
}


This code is to kill all the thread on failure of any one thread.
if(fail_flag>0)
{
for (IThreadCount=0;IThreadCount<IThreads;IThreadCount++)
{
GetExitCodeThread (hThreadHandle[IThreadCount],&ExitStatus);
if(ExitStatus != 0)
{
ExitThread(ExitStatus);
}
}
return FALSE;
}
else
{
return TRUE;
}

waiting for the reply
 
ExitThread ends the thread which calls ExitThread normally. It does NOT stop other threads.
If you want a thread to be killed by another thread, you have to call TerminateThread.
But, calling TerminateThread should only be done in critical situations when you have no other choice: the thread to be killed does not get any chance to free its resources. If the thread owns a CRITICAL_SECTION, it is never leaved, possibly causing the entire program to freeze, and, on 9x/ME, possibly causing blue screens of death.

Fortunately, there is another way to force a thread to terminate:
1. Check if the thread is still running. If yes, continue with step 2:
2. Suspend the thread.
3. Get the context of the thread
4. Modify the instruction pointer in the thread context, let it point to a function which frees resources and calls ExitThread
5. Write back the thread context
6. Resume the thread
7. Wait a limited time for the thread to end; If it has not stopped in a reasonable amount of time, there is no option left other than calling TerminateThread

In code this will be:

Code:
// This routine is called if the thread is forced to end:
void ThreadForcedEnd ( )
{ DWORD dwID = GetCurrentThreadId ( );
  // With this thread-id, unique to each thread,
  // you can look up it's resources and free them here
  ExitThread ( 1 ); // This is a normal thread end!
}

void MyTerminateThread ( HANDLE hThread )
{
  DWORD dwExitCode;
  if ( !GetExitCodeThread ( hThread, &dwExitCode ))
     { // Probably hThread wrong
       return; }

  if ( dwExitCode != STILL_ACTIVE )
     { // Thread has already ended
       return; }

  if ( SuspendThread ( hThread ) == -1 )
     { // Failed to suspend the thread
       TerminateThread ( hThread, 2 );
       return; }

  // The thread is now &quot;sleeping&quot;
  
  // Get the context of the thread

  CONTEXT ThreadContext;
  ThreadContext.ContextFlags = CONTEXT_CONTROL;
  if ( !GetThreadContext ( hThread, &ThreadContext ))
     { // failed to get the thread context
       TerminateThread ( hThread, 3 );
       return; }

  // change the instruction pointer of the thread
  // Let it point to ThreadForcedEnd

  ThreadContext.ContextFlags = CONTEXT_CONTROL;
  ThreadContext.Eip = (unsigned long)&ThreadForcedEnd;

  // Write back the thread context

  if ( !SetThreadContext ( hThread, &ThreadContext ))
     { // failed to write back new instruction pointer
       TerminateThread ( hThread, 4 );
       return; }

  // Now, wake up the thread, it will call ThreadEndForced

  while ( ResumeThread ( hThread ) > 1 ) { }

  // Wait max. 5 seconds for the thread to end

  if ( WaitForSingleObject ( hThread, 5000 ) == WAIT_TIMEOUT )
     { // 5 seconds timed out, thread is still running
       TerminateThread ( hThread, 5 );
       return; }

  // OK, thread has ended normally and freed it's resources !!
}
Marcel
 
Thank you for solving my problem.your code helped me very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top