Stopping/canceling a running database query
-
Hi folks, what would be a good way to make a database query cancelable? Purpose is that the user should abort the function if it takes too long. I've already stuffed the whole database access logic into a separate thread. When the user clicks the abort button then the Thread.Abort function is called. Problem 1: The thread ignores the Abort call until the query is completely processed. Problem 2: Somehow I cannot fire a second query while the first hasn't finished (e.g. User quickly clicks Search->Cancel->Search). I'm not sure why - it might be the underlying Access database (please don't mock me for that - the db is set by the customer). I'm working with .Net 1.1. Any ideas?
-
Hi folks, what would be a good way to make a database query cancelable? Purpose is that the user should abort the function if it takes too long. I've already stuffed the whole database access logic into a separate thread. When the user clicks the abort button then the Thread.Abort function is called. Problem 1: The thread ignores the Abort call until the query is completely processed. Problem 2: Somehow I cannot fire a second query while the first hasn't finished (e.g. User quickly clicks Search->Cancel->Search). I'm not sure why - it might be the underlying Access database (please don't mock me for that - the db is set by the customer). I'm working with .Net 1.1. Any ideas?
Within your data access class, store a boolean variable such as
Cancelling
. Now, read your data in chunks using something like aDataReader
, except after reading each chunk of data check whether the Cancelling flag has been set to true. If it has, close your data reader and connection and return from the method as quickly as possible. You can now do exactly as you were, starting that data access on its own thread. When you want to cancel, set the flag in your data access class to true andThread.Join
the data access thread before firing off a new request.In this way, noThread.Abort
will be required. -
Within your data access class, store a boolean variable such as
Cancelling
. Now, read your data in chunks using something like aDataReader
, except after reading each chunk of data check whether the Cancelling flag has been set to true. If it has, close your data reader and connection and return from the method as quickly as possible. You can now do exactly as you were, starting that data access on its own thread. When you want to cancel, set the flag in your data access class to true andThread.Join
the data access thread before firing off a new request.In this way, noThread.Abort
will be required.A DataReader won't help me in this situation because the request normally returns only one row. Thus the first DaraReader.Read call will block. Any other ideas?