views:

505

answers:

2

Possible Duplicate:
How to stop long executing threads gracefully?

Hello.

I have a background thread which needs to perform an operation, it works fine all of the time except in one case : when the resource is corrupted. When that happens the thread gets Blocked in the Load (to that resource) calls in the Execute method.

When that happens the thread Won't respond to the Terminate method ( call from main thread ) and gets blocked.

So, my question is : How to properly terminate the blocked thread ( from the main thread ). And no I cannot modify the class which loads the resource, or neither know from before if the resource is corrupted or not.

+2  A: 

Similar question is here

SimaWB
+1 it is a duplicate
Jeroen Pluimers
Welcome to Stack Overflow, Sima. Your response should really have been a *comment* attached to the question.
Rob Kennedy
+1  A: 

Look for TerminateThread() WinAPI function. Some useful explanation can be found here or look at MSDN documentation.

Of course, after terminating you must look if any resources allocated in thread not freed and free it appropriately.


Update

Yes, using TerminateThread is bad practice (as specified in comments). I'm agree with this opinion. But "never use it, even if you really need to use it" recomendation it too strong from my point of view and very theoretic. Real world full of design flaws and buggy 3rd-party libraries.
Information, given in question not enough for making right decision about this concrete situation. E.g. it may be temporary workaround with no alternatives, etc.
Therefore, from theoretic point of view right answer is : "There are no way to terminate process properly if you can't control how to "freezing" step in background thread processed."
From practical point of view right answer is: "There are no way to terminate process properly if you can't control how to "freezing" step in background thread processed. But if you realize that you can't, but still needs such functionality - use TerminateThread() API call"

About TerminateThread vs. TerminateProcess:
- Creating/terminating process requires more resources than creating/terminating thread
- Creating/terminating process more complicated => more place for bugs
- TerminateProcess don't terminates immediately and waits for I/O operations to complete (MSDN) => not a choice for scenario where remote shared folder becomes unavailable while reading and other similar I/O scenarios.
- Creating and terminating process requires more user privileges than creating thread, compare MSDN here and here

About resource freeing:
Thread stack freed automatically when terminating thread (as mentonied in MSDN). Resources is primarily resources, allocated by main thread for communication with background thread. E.g. memory structures, mutexes, etc.

ThinkJet
Right, *look at* that function, and then conclude that you should never, ever call it. You can't free the resources allocated by the thread because you have no idea what they are or how to free them. (How do you free the thread's stack, for instance?)
Rob Kennedy
This question *is* the same. Whether you have access to the source code doesn't matter — the restrictions are the same in both questions: the code can't be modified, and we can't know in advance whether the thread will need to be stopped. Furthermore, the top answer to the other question also applies to this one. The best thing to do is to modify the program so the thread doesn't get blocked in the first place. If that's not possible, then move the operation to a separate process. It's much safer to terminate a whole process than to terminate just one thread.
Rob Kennedy