cancelling a backgroundworker

B

BillE

I am using a backgroundworker to execute a lengthy SQL Server stored
procedure.

There is no opportunity for the DoWork procedure to test the
CancellationPending property, so I can't cancel the operation.

What I would like to do instead is terminate the backgroundworker if a
cancel button is clicked.

Is there a good way to accomplish this?

Thanks
Bill
 
T

Tom Shelton

I am using a backgroundworker to execute a lengthy SQL Server stored
procedure.

There is no opportunity for the DoWork procedure to test the
CancellationPending property, so I can't cancel the operation.

What I would like to do instead is terminate the backgroundworker if a
cancel button is clicked.

Is there a good way to accomplish this?

You would probably need to call CancelAsync and then close the DB Connection -
to force the code to transition back to managed code.

This would most likely cause an sql exception to occure, so, you'll want to
make sure your swallowing the exceptions when the CancellationPending property
is true... You could probably use an exception filter for that.

Just an idea.
 
R

Raja Venkatesh

Step 1. In the start of your form put "CheckForIllegalCrossThreadCalls = True"
Step 2. Set the "BackGroundWorker.WorkerSupportsCancellation = True" property
Step 3. Handle cancelAsync event of the object.
 
B

BillE

There is no opportunity for the DoWork procedure to test the
CancellationPending property, so I can't cancel the operation.

Thanks
Bill
 
A

Andrew Morton

BillE said:
I am using a backgroundworker to execute a lengthy SQL Server stored
procedure.

There is no opportunity for the DoWork procedure to test the
CancellationPending property, so I can't cancel the operation.

What I would like to do instead is terminate the backgroundworker if a
cancel button is clicked.

Is there a good way to accomplish this?

So, what you're /really/ after is a way to cancel an SQL Server query, like,
ooh, I don't know, SqlCommand.Cancel?

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top