// JP opened flex table

Click to See Complete Forum and Search --> : TerminateThread


dellthinker
October 12th, 2007, 01:24 AM
Hi all. Does TerminateThread have to be inside the same funtion as CreateThread does in order to terminate properly? Thanx in advance!

Swapnil_
October 12th, 2007, 01:32 AM
no there is no need to call TerminateThread from function calling CreateThread.infact if u will do this then as soon as thread will start it will stop doing nothing.

While calling TerminateThread u only need the handle of the thread which u want to terminate

cilu
October 12th, 2007, 02:14 AM
I suggest reading this FAQ: http://www.codeguru.com/forum/showthread.php?t=305166.

dellthinker
October 12th, 2007, 09:02 AM
I suggest reading this FAQ: http://www.codeguru.com/forum/showthread.php?t=305166.


I did. I read that over and over. And i cant get it right.

This is what i did.


void function1(void){
HANDLE thread[1];
thread[8]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)function,&a,0,0);
}

void function2(void){
HANDLE thread[1];
TerminateThread(thread, 0); // Just so we're clear as to what im trying
}


Both API calls are in different functions. I tried to terminate the threads handle just like i read from Andreas Masur's post but it didnt work.

Again, the CreateThread and TerminateThread API calls are not in the same function. So the thread starts up with the program among other threads, but i need it to terminate at my will. Any suggestions?

wildfire99
October 13th, 2007, 06:39 AM
Can you just use a variable that the worker thread checks periodically (with whatever single-access protection mechanism you like) to see if it should exit? Your main thread can set it to a certain value that tells the worker to quit, and thus it can end itself cleanly (and close its own handle if that's required). You can then move on while your thread thrashes about until it decides to die (unless you really need to WaitForSingleObject() and make sure it is dead). Dirty and simple. :)

Marc G
October 13th, 2007, 07:16 AM
[ moved thread ]

dellthinker
October 13th, 2007, 06:05 PM
No worries, i figured it out.

Just simply made the HANDLE a global variable for the source code i had it in and called TerminateThread() in the same source and it works fine ;)

I just thought i'd post my solution so that in case anyone googling for the same problem may run into it that might have the same problem i did.

Arjay
October 13th, 2007, 09:50 PM
No worries, i figured it out.

Just simply made the HANDLE a global variable for the source code i had it in and called TerminateThread() in the same source and it works fine ;)

I just thought i'd post my solution so that in case anyone googling for the same problem may run into it that might have the same problem i did.This may work, but it isn't the recommended approach. TerminateThread as it should only be used as a last resort. If you need your thread to exit, all you need to do is check for an event inside your thread. Use WaitForSingleObject with a timeout of 0 and when the event gets set, exit the thread.

Please read the Simple Thread: Part I article in my subject line. It illustrates how to create, start, pause, resume and stop a thread. Check it out, and let me know if I can improve the article. The article uses the event technique I mentioned.

dellthinker
October 16th, 2007, 01:48 PM
This may work

Exactly! And it works very fine i might add. My goal is to accomplish a task and move on. Im not in class and im not getting paid for this....... So if it looks bad to someone else thats fine. As long as it works.

P.s. What article are you talking about?

Arjay
October 16th, 2007, 03:25 PM
Exactly! And it works very fine i might add. My goal is to accomplish a task and move on. Im not in class and im not getting paid for this....... So if it looks bad to someone else thats fine. As long as it works.My suggestion not to use TerminateThread is equivalent to not closing handles or freeing up allocated memory. Sure it takes more time to learn how to do these things, but once learned, it takes very little extra time to do things properly. Also, once you graduate and are working, you will be more useful if you already are writing solid code.

P.s. What article are you talking about?You'll see several blue links to articles listed just below my name 'Arjay'.

//JP added flex table