How to stop thread in ThreadPool?

If you really need to abort this Thread manually, then you can use the Thread class. For details about how to use it, you can look at this online resource.

If you really need to abort this Thread manually, then you can use the Thread class. For details about how to use it, you can look at this online resource. Please note that usually, using Thread.Abort(); is not a best practice, except when you are ending your program and wants to terminate all the running threads.In your case, as you are trying to end a SQL query, you should look at other ways to stop it (SqlCommand.Cancel(), ISession.CancelQuery(), ...).

If you want to Cancel a SQL command, have a look at SqlCommand. Cancel documentation.

Consider using the BackgroundWorker class. It supports canceling. But you can only cancel the query in a safe manner, if the database provider supports canceling it.

Your code doesn't look like it supports canceling. Update: After some discussion, the following info also is worth to be put into the answer: SqlCommand supports cancellation as Guillaume points out, but the database driver used needs to also support it. So best way to do it: Use SqlCommand or the class derived from it for your DBMS, execute the query and try to cancel it and see whether it is supported.

If it is not supported, you will get an exception. If it is supported, the example for SqlCommand. Cancel will help you to implement the behavior you want.

1 But it does not support cancellation from within a database query. It supports cancellation in the sense that you can inject cancellation checks into your code and act apropriately. – R.

Martinho Fernandes Feb 22 at 10:44 Yes. That's what I am saying with the rest of my answer :-) – Daniel lgarth Feb 22 at 10:45 How can it help? It supports cancellin via flag 'CancellationPending'.

If I execute long SQL query, how can I check this flag? – Lari13 Feb 22 at 10:45 1 Your main problem is, that your database provider doesn't seem to support cancellation or async processing. Having said that, the only way to stop the execution of the query seems to be to kill the thread it is running in.

You can't do that with ThreadPool, see here: social.msdn.microsoft. Com/Forums/en-SG/csharpgeneral/thread/…. You would need to use the normal Thread class and do all the work yourself.

– Daniel lgarth Feb 22 at 10:48 1 Thread. Abort is a bad idea. If your provider doesn't support cancellation change provider.

If you can't change it... well you are screwed Thread. Abort will raise funny bugs. – Guillaume Feb 22 at 11:08.

You can also add a timeout value for the SQLCommand. Aka, cancel after x seconds if not finished.

This doesn't help. User wants to get query results or can change own mind and execute other query/task. – Lari13 Feb 24 at 6:34 Like someone else said, you may need to use a manageable thread instead of a pool thread.

Pool threads are really meant for short lived threads where the cost of creating and destroying a thread isn't worth the work being done. Also, if your connection doesn't support canceling, you're kind of SoL. If you need to cancel a query, then use a DB that supports canceling.

– Bengie Feb 24 at 17:08.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions