Solved

Killing a Thread outside the thread function

Posted on 1998-06-12
4
419 Views
Last Modified: 2013-11-20
I want to kill a thread, AfxBeginThread(...). It is given that I can do so within the thread. I want to kill on pressing a command button.
Jalaja
0
Comment
Question by:aac
  • 2
4 Comments
 

Accepted Solution

by:
zhaowei earned 50 total points
ID: 1317910
You can use TerminateThread after you get the handle of thread.

0
 
LVL 2

Expert Comment

by:rayb
ID: 1317911
You must be careful when using the TerminateThread API.  When this function is called the thread will have no opportunity to perform any cleanup whatsoever.
Often a better solution is to stash an abort flag somewhere, perhaps in your CWinApp object.  The thread should have code to check this flag every once in a while and the command button can set the flag.

0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1317912
If your child thread is spending a lot of time waiting (with WaitForSingleObject() or simular), you could create a global manually event.

Modify you thread function to take the event into account:

----

extern HANDLE g_hAbortThreadSignal;

void MySmartThreadFunc(LPVOID lpvParam)
{
   [....]

   HANDLE hWaitHandles[] = { hSomeHandle, g_hAbortThreadSignal };
   while(1)
   {
        DWORD dwResult = WaitForMultipleObjects(2, hWaitHandles, FALSE, INFINITE);

        if(dwResult == (WAIT_OBJECT_0+1))
        {
            // Our abort signal set, exit thread function
            return 0;
        }
       
        //
        // Normal processing goes here!
        //
    }
    return 0;
}
   
----

// To create the manual reset event insert this in CWinApp::InitInstance() or simular:

HANDLE g_hAbortThreadSignal;
g_hAbortThreadSignal = CreateEvent(NULL, TRUE, FALSE, NULL);

// Remember to destroy the event when exiting the app:
CloseHandle(g_hAbortThreadSignal);

---

// To make the child thread exit, just signal the event from somewhere using the SetEvent() function:
SetEvent(g_hAbortThreadSignal);

// (Remember to reset it again if you start the child thread again!)
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1317913
Mistake, mistake.... The thread function should not return anything. Replace 'return 0' with just 'return'.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 96
Hibernate methods 2 77
Window placement 17 87
MaxSpan challenge 9 106
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question