I start a long operation (usually long query (SELECT) to database).
It might take long time, and I want to add capability to stop this action.
Code looks like:
ThreadPool.QueueUserWorkItem(a => action.DoLongAction());
public void DoLongAction()
{
var sql = Tables.Where(el => el.Some == "XXX"); // dynamically constructed LINQ query to database
var result = db.Query(sql);
}
I can’t use signals of flags, to stop this thread. Because, long operation isn’t in my code, but in database process (sql query about 5 mins).
How can I stop this thread correctly?
UPD1. Not plain SQL, but LINQ (EF4, BLToolkit)
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(), …)