Store Procedure in Thread (ASP.NET)


I have a store procedure that can take like 10 - 15 minutes.
I want to execute the SP in a Thread, because the user can cancel the query.

I try with Dim MyThread As New Thread(AddressOf runQuery)

So i put a start button (MyThread.Start()) an a cancel but (MyThread.Abort(),, i guest...)

My query runs fine, but i can't cancel de thread..

I'm using ASP.NET, with a SQL Server 2000 DB. And a WebService have the logic of the app...

WebForm -> WService -> VB Class -> DB

Any help please?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Cancelling a thread that executes a stored procedure is a difficult task as the SP is run in a different process that is controlled by the DB server. So although the "thread" that started the execution of the thread the procedure would still execute.

However to know the inner workings of a thread.abort(), what happens is on calling abort it just raises a thread abort exception that is raised again after your catch block has executed. but again the finally block is excuted and then the thread is stopped.

so this is what you could do:

put a try catch block in the method that the thread would execute.

add a thread.join() immediately after the thread.abort() call. this would block your current thread until the aborted thread has stopped.

however just to let u know.. that wont abort the SP executing

Alucard_crAuthor Commented:
Thanks vippx, I'll do that and try a little more.

I'll wait for more comments...

Thank you
Closing the database connection should inform the database server to cancel executing the stored procedure.
Alucard_crAuthor Commented:
I'll try to cancel the query. Maybe a Command.Cancel() or with the DataAdapter...??
Yes ... the command.cancel() should cancel the command. but make sure that appropriate rollbacks are in place. too often, killed SPs are real messy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.