cancelling a backgroundworker

  • Thread starter Thread starter BillE
  • Start date Start date
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
 
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.
 
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.
 
There is no opportunity for the DoWork procedure to test the
CancellationPending property, so I can't cancel the operation.

Thanks
Bill
 
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
 
Back
Top