Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort a thread which is running a long query

I have a thread which calls one of the methods, now this method executes a query which can take a very long time possibly 40 minutes or so to complete,

I want to give user a a choice to be able to cancel this operation (meaning stop the thread and stop the query to release database).

I should mention that I am developing WPF Application using .net 4.5, SQL SERVER DB and C#.

like image 464
Abdullah Malikyar Avatar asked Jun 06 '15 06:06

Abdullah Malikyar


1 Answers

You should use backgroundworker, it is exactly what you want.

Eather drag and drop it from the toolbox or create it in code - behind. It supports Cancellation, reports progress, notifies when complete and know if it is running or not.

Here is an example.

void method(){
        BackgroundWorker worker = new BackgroundWorker();
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.ProgressChanged += worker_ProgressChanged;
        worker.DoWork += worker_DoWork;
        worker.WorkerSupportsCancellation = true;
        if(!worker.IsBusy)
        {
            worker.RunWorkerAsync();
        }
}

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        //do whatever needs to be done on the other thread here.
        object argument = e.Argument; //if passed argument in RunWorkerAsync().
        object result = new object();
        e.Result = result;
        //after making worker global, you can report progress like so:
        worker.ReportProgress(50); //you can also pass a userState, which can be any object, to show some data already.
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        //you can update a progress bar in here
        int progress = e.ProgressPercentage;

    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //when done
    }

    void CancelTheTask()
    {
        if (worker.IsBusy)
        {
            //make worker global first, but then
            worker.CancelAsync();
        }
    }

A important things to look at: Never use resources in the DoWork method that are not created inside it. Thus pass things you need in the background worker as Arguments. And things that are created by the backgroundworker should not be set to a global variable ether, pass by result.

When cancelling, RunWorkCompleted will also be fired. Now the query to the database is already being executed, so that is still running, even when your application lost all resources to it.

To cancel that, we would need to know how you execute the query, like @S.Akbari mentioned is one way. Entity Framework 6 also supports cancellation.

For that: check this when using Queryable

here is another example

Or this solution without Entity Framework.

like image 153
CularBytes Avatar answered Sep 26 '22 14:09

CularBytes