Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

c# - 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#.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...