中止正在运行长查询的线程 [英] Abort a thread which is running a long query

查看:93
本文介绍了中止正在运行长查询的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用其中一个方法的线程,现在该方法执行一个查询,该查询可能需要很长时间才能完成,可能需要40分钟左右的时间,

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).

我应该提到我正在使用.net 4.5,SQL SERVER DB和C#开发WPF应用程序.

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

推荐答案

您应使用

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.

这里是一个例子.

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();
        }
    }

要注意的重要事项:切勿使用DoWork方法中未在其中创建的资源.因此,在后台工作程序中将您需要的内容作为参数传递.而且,由背景工作人员创建的内容不应设置为全局变量以太,而应通过结果传递.

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.

取消时,也会触发RunWorkCompleted.现在,对数据库的查询已经在执行,因此即使您的应用程序丢失了所有资源,查询仍在运行.

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.

要取消该查询,我们需要知道如何执行查询,例如提到的 @ S.Akbari 道路.实体框架6还支持取消.

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.

为此:在使用时进行检查可查询的

这是另一个示例

或者没有实体框架的解决方案.

这篇关于中止正在运行长查询的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆